gpt4 book ai didi

c# - 在组合框中实现 IDataErrorInfo

转载 作者:太空狗 更新时间:2023-10-29 20:30:43 26 4
gpt4 key购买 nike

我在使用 IDataErrorInfo 验证 ComboBox 时遇到问题。

我设置了 1 个文本框和 1 个组合框,在运行程序时,第一个焦点在文本框上,当我点击 Tab 以聚焦在组合框中时,我得到:

InvalidOperationException: 'validationTooltip' name cannot be found in the name scope of '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" />

这是在数据绑定(bind)组合框中使用的 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; }
}

任何帮助将不胜感激。请让我知道我可以添加的任何内容以使其更清楚。

最佳答案

我之前遇到过同样的问题,起初我怀疑我与 ComboBox SelectedValue 的绑定(bind)导致了问题。我尽我所能来调试程序,但没有帮助。直到我发现问题/错误出在 mahApps 上。以下是解决问题的一些步骤:

  1. 在您的项目中卸载/移除 mahApp。重新构建您的项目,让我们看看您是否仍然遇到同样的错误。

    1.1。如果问题仍然存在,请转到第 2 步。如果不是,请继续执行第 1.2 步。

    1.2。如果通过删除 mahApps 解决了问题,您可以选择其他布局包。 :)) 或者如果你真的想使用 mahApps。请忽略步骤号。 2 并继续第 1 步。 3

  2. 如果问题仍然存在,请尝试在 Visual Studio 2013 中重新迭代您的解决方案。您可以下载 here .如果您已经在使用 VS2013,请继续第 3 步。
  3. 重新安装 mahApps(确保您已经删除旧 mahApps 的所有 .dll 和包)。转到程序包管理器控制台键:安装包 MahApps.Metro -Pre
  4. 做使用 mahApps 所需的事情。在窗口标签关闭之前,即。 </Controls:MetroWindow> ,确保你有这个:

    <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. 重新构建您的应用程序,让我们看看您得到了什么。

简答:完全卸载 mahApps(即删除所有 DLL 和包)将解决此问题。完全卸载 mahApps 后,如果您想重试,可以通过 NuGet 或包管理器安装全新的 mahApps。按照说明 here .如果全部失败,请更新您的 VS,然后再次尝试更新 mahApps。

希望对您有所帮助!

关于c# - 在组合框中实现 IDataErrorInfo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22666962/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com