Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing IDataErrorInfo in combobox

Tags:

c#

mvvm

wpf

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.

like image 962
neo Avatar asked Mar 26 '14 16:03

neo


1 Answers

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:

  1. 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

  2. If the issue persists, try to re-iterate your solution in Visual Studio 2013. You can download here. If you already using VS2013, continue to step no.3.
  3. Re-install mahApps (make sure you already remove all the .dlls and packages by the old mahApps). Go to Package Manager Console key in: Install-Package MahApps.Metro -Pre
  4. 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>
    
  5. 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!

like image 97
jomsk1e Avatar answered Sep 28 '22 19:09

jomsk1e