Embedding IDataErrorInfo in combobox

I have a problem checking ComboBox using IDataErrorInfo .

I set 1 text box and 1 combobox, after starting the program, the first focus is in the text box, when I click on the tab to focus in the drop-down list that I get:

InvalidOperationException: The name "validationTooltip" cannot be found in the namespace "System.Windows.Controls.ToolTip".

To help you here help in 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 is the ProductModel.cs that is used in the data binding binding:

 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 is MainWindowViewModel.cs , which 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 greatly appreciated. Please let me know everything that I can add from my end to make it more understandable.

+7
c # wpf mvvm
source share
2 answers

I have the same question, first of all, I suspected that my binding to the ComboBox SelectedValue was causing the problem. I tried everything I could, debug the program, but did not help. Until I found out that the problem / error is on mahApps. Here are some troubleshooting steps:

  • Remove / remove mahApps in your project. Restore your project and make it clear that you have not made the same mistake.

    1.1. If the problem persists, go to step No. 2. If you do not continue from step 1.2.

    1,2. If the problem is fixed by uninstalling mahApps, you can select other build packages there. :)) Or if you really want to use mahApps. Please ignore step no. 2 and continue with step No. 3

  • If the problem persists, try iterating your solution in Visual Studio 2013. Here you can download here . If you are already using VS2013, go to step 3.
  • Reinstall mahApps (make sure you have already deleted all the DLL files and packages with old mahApps). Go to the package manager console in: Install-Package MahApps.Metro -Pre
  • Do the things necessary to use mahApps. Before closing the window tag, i.e. </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> 
  • Restore your application, see what you have.

Short answer: Cleaning up mahApps removal (i.e. removing all DLLs and packages) will fix the problem. After cleaning up the mahApps utility, if you want to try again, you can install a new new mahApps package through NuGet or through the package manager. Follow the instructions here . If all else fails, update VS and try updating mahApps again.

Hope this helps!

+3
source share

Try using triggers to display validation errors and set the Validation.ErrorTemplate parameter of the combo box to 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> 
+1
source share

All Articles