Implementing IDataErrorInfo in combobox
I am having a problem validating a ComboBox
using IDataErrorInfo
.
I set up 1 textbox and 1 combobox, upon running the program the first focus is on textbox, when I hit tab to focus in the combobox I am getting:
InvalidOperationException: 'validationTooltip' name cannot be found in the name scope of 'System.Windows.Controls.ToolTip'.`
To help you helping me here's some part of my XAML:
<Window.DataContext>
<ViewModels:MainWindowViewModel/>
</Window.DataContext>
<!-- Batch ID-->
<Label Content="Batch ID"
Height="28"
Margin="64,52,191,0" VerticalAlignment="Top" />
<TextBox Name="txtBatchId"
Text="{Binding BatchId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
Margin="124,52,65,0" TabIndex="1" Height="26" VerticalAlignment="Top" />
<!-- Product -->
<Label Content="Product"
Height="28" Margin="54,81,191,0" VerticalAlignment="Top" />
<ComboBox Name="cmbProduct"
ItemsSource="{Binding Products}"
DisplayMemberPath="ProductName"
SelectedValuePath="ProductId"
SelectedValue="{Binding SelecteProductId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
Height="23" Margin="124,81,65,0" VerticalAlignment="Top" TabIndex="2" />
Here's ProductModel.cs
that is used in databinding combobox:
public class ProductModel
{
public int ProductId {get;set;}
public int ProductName {get;set;}
public ProductModel(int prodId, string prodName)
{
ProductId = prodIdl;
ProductName = prodName;
}
}
And here's the MainWindowViewModel.cs
that implements INotifyPropertyChanged and IDataErrorInfo:
public class MainWindowViewModel : ViewModelBase, IDataErrorInfo
{
private string _batchId;
public string BatchId
{
get { return _batchId; }
set
{
_batchId = value;
OnPropertyChanged("BatchId");
}
}
private ObservableCollection<Product> _products = new ObservableCollection<Product>();
public IEnumerable<Product> Products {
get { return _products; }
}
private string _selectedProductId;
public string SelectedProductId
{
get { return _selectedProductId; }
set
{
_selectedProductId = value;
OnPropertyChanged("SelectedProductId");
}
}
public void PopulateProduct() {
....
}
public MainWindowViewModel()
{
PopulateProduct();
}
public string this[string columnName]
{
get
{
string result = string.Empty;
switch (columnName)
{
case "SelectedProductId":
if (SelectedProductId == null || SelectedProductId == "0")
{
result = "Please select a product";
}
break;
case "BatchId":
if (string.IsNullOrWhitespace(BatchId))
{
result = "Please input batch id";
}
break;
}
return result;
}
}
public string Error { get; private set; }
}
Any help would be highly appreciated. Please let me know anything I can be added from my end to make it more clear.
I've got on the same issue before, at first I suspected that my binding with the ComboBox SelectedValue is causing the problem. I tried all I can to debug the program but wasn't help. Until I found out that the issue/bug is on mahApps. Here's some steps to troubleshoot your problem:
Uninstall/Remove mahApps in your project. Re-build your project and let's see if you still encounters the same error.
1.1. If the issue persists, Go to step no.2., if not continue with step 1.2.
1.2. If the issue is fixed by removing mahApps, you can choose other layouting packages out there. :)) Or if you really want to use mahApps. Please ignore step no. 2 and continue with step no. 3
Do the things needed to use mahApps. Before the closing of window tag ie. </Controls:MetroWindow>
, make sure you have this:
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
Re-build your application, let's see what you've got.
Short answer: Clean uninstall of mahApps (ie. removal of all DLLs and packages) will fixed the issue. After clean uninstall of mahApps, if you want to try again, you can install a fresh new installment of mahApps via NuGet or via Package Manager. Follow the instructions here. If all fails, update your VS then try again the update of mahApps.
Hope that helps!
尝试使用触发器来显示验证错误并将组合框的Validation.ErrorTemplate设置为null:
<Style x:Key="comboBoxInError" TargetType="{x:Type ComboBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={x:Static RelativeSource.Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
<Setter Property="Validation.ErrorTemplate" Value={x:Null} />
</Trigger>
</Style.Triggers>
</Style>
链接地址: http://www.djcxy.com/p/78396.html
上一篇: 如何用IntelliJ设置libdgx?