在组合框中实现IDataErrorInfo

7
我遇到了使用IDataErrorInfo验证ComboBox的问题。
我设置了1个文本框和1个组合框,在运行程序时,首先将焦点置于文本框,当我按tab键将焦点置于组合框时,出现以下错误:
“InvalidOperationException: 'validationTooltip'名称在'System.Windows.Controls.ToolTip'的名称范围内找不到。”
为了帮助您帮助我,这里是我的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" />

以下是在数据绑定组合框中使用的ProductModel.cs:

这是在数据绑定组合框中使用的ProductModel.cs

public class ProductModel
{
    public int ProductId {get;set;}
    public int ProductName {get;set;}

    public ProductModel(int prodId, string prodName)
    {
        ProductId = prodIdl;
        ProductName = prodName;
    }
}

这里是实现了INotifyPropertyChanged和IDataErrorInfo的MainWindowViewModel.cs代码:

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; }
}

非常感谢您的帮助。请让我知道我可以做些什么来使它更加清晰易懂。


1
请展示定义validationTooltip的模板。 - Michael G
我没有为validationToolTip分配模板。只是一句话,我正在使用mahApps.Metro。 - neo
请查看此链接:https://github.com/MahApps/MahApps.Metro/issues/134 - dkozl
我已经访问了那个链接,更新了mahApps,按照链接中建议的删除了一些片段,但仍然没有运气。 :( 我被卡在这里整整一天了。 - neo
2个回答

3

我之前也遇到过同样的问题,起初我怀疑与ComboBox的SelectedValue绑定有关,尝试了各种程序调试但都没有解决问题。直到我发现这个问题/bug是由mahApps引起的。以下是一些故障排除步骤:

  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.

简短回答: MahApps的完全卸载(即删除所有DLL和软件包)将解决该问题。在干净卸载MahApps之后,如果您想再次尝试,则可以通过NuGet或Package Manager安装全新的MahApps安装程序。请按照此处的说明操作。如果仍然失败,请更新您的VS,然后再尝试更新MahApps。
希望能帮到您!

谢谢!这是一个好方法!起初我尝试在不卸载的情况下更新Mahapps,但错误仍然存在。我尝试了干净卸载Mahapps,删除所有dll和包,然后通过包管理器控制台安装最新的包,问题得到解决!非常感谢! - neo

1
尝试使用触发器来显示验证错误,并将组合框的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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接