gpt4 book ai didi

wpf - IDataErrorInfo - 即使收到一条错误消息,也看不到任何错误消息

转载 作者:行者123 更新时间:2023-12-03 10:18:39 25 4
gpt4 key购买 nike

我有 ItemType ,它在 IDataErrorInfo 接口(interface)的帮助下实现了验证所需的一切:

#region IDataErrorInfo implementation
//WPF doesn't need this one
public string Error
{ get { return null; } }

public string this[string propertyName]
{
get { return GetValidationError(propertyName); }
}
#endregion

#region Validation
public bool IsValid
{
get
{
foreach (string property in ValidatedProperties)
{
if (GetValidationError(property) != null)
{
return false;
}
}

return true;
}
}

static readonly string[] ValidatedProperties =
{
"Name"
};

private string GetValidationError(string propertyName)
{
if (Array.IndexOf(ValidatedProperties, propertyName) < 0)
return null;

string error = null;

switch (propertyName)
{
case "Name":
error = ValidateName();
break;
default:
Debug.Fail("Unexpected property being validated on Customer: " + propertyName);
break;
}

return error;
}

string ValidateName()
{
if (!IsStringMissing(Name))
{
return "Name can not be empty!";
}

return null;
}

static bool IsStringMissing(string value)
{
return string.IsNullOrEmpty(value) ||
value.Trim() == String.Empty;
}
#endregion

ItemType 用 ItemViewModel 包装。在 ItemViewModel 上,当用户单击“保存”按钮时,我有一个命令:
public ICommand SaveItemType
{
get
{
if (saveItemType == null)
{
saveItemType = new RelayCommand(() => Save());
}

return saveItemType;
}
}

然后,在 DetailsView 中,我有以下 xaml 代码:
<TextBlock Text="Name:" />
<TextBox Grid.Column="1" Name="NameTextBox" Text="{Binding Name, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
Validation.ErrorTemplate="{x:Null}" />

<ContentPresenter Grid.Row="13" Grid.Column="2"
Content="{Binding ElementName=NameTextBox, Path=(Validation.Errors).CurrentItem}" />

下面的架构是怎么回事(不清楚,但是form其实是一个独立的xaml文件(User Control),其中form中grid的datacontext设置为ObservableCollection):
<Grid DataContext="{Binding Items}">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>

enter image description here

我遇到的问题是没有显示错误消息。当我断点并检查它是否正确验证以及是否有任何错误消息时,我确实有它们。但不知何故,错误消息没有到达 xaml。

拼图中缺少的部分是什么?

编辑 - 丢失的部分

对,所以,它是以下内容:

我在我的模型上实现了 IDataErrorInfo,但没有在包装模型的 ViewModel 上实现。我还要做的是在 ViewModel 上实现 IDataErrorInfo 接口(interface),并从模型中获取它。

IDataErrorInfo 的 ViewModel 实现:
{ get { return (ItemType as IDataErrorInfo).Error; } }

public string this[string propertyName]
{
get
{
return (ItemType as IDataErrorInfo)[propertyName];
}
}

最佳答案

我使用以下样式来查看我的验证是否发生。

<Style x:Key="{x:Type TextBox}" TargetType="{x:Type TextBox}">
<Style.Triggers>>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding Path=(Validation.Errors).CurrentItem.ErrorContent, RelativeSource={x:Static RelativeSource.Self}}"/>
<Setter Property="Background" Value="{StaticResource BrushErrorLight}" />
</Trigger>
</Style.Triggers>
</Style>

您应该在工具提示中看到验证消息

编辑:

尝试将 NotifyOnValidationError=true 添加到您的绑定(bind)中
<TextBox Grid.Column="1" Name="NameTextBox" 
Text="{Binding Name, ValidatesOnDataErrors=True, NotifyOnValidationError=true, UpdateSourceTrigger=PropertyChanged}" />

关于wpf - IDataErrorInfo - 即使收到一条错误消息,也看不到任何错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5788287/

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