c# - 如何在WPF文本 block 验证错误上显示消息框

标签 c# wpf xaml mvvm messagebox

我已经使用IDataErrorInfo对Datagrid单元模板进行了验证,并且能够显示工具提示错误消息。如何显示带有错误信息的消息框而不是工具提示?

  <DataGridTemplateColumn Header="Code" MinWidth="150">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Code, ValidatesOnDataErrors=True, NotifyOnValidationError=True}" Style="{StaticResource TextBlockStyle}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Code, UpdateSourceTrigger=LostFocus}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
  </DataGridTemplateColumn>

  <Style x:Key="TextBlockStyle" TargetType="{x:Type TextBlock}">
           <Style.Triggers>
              <Trigger Property="Validation.HasError" Value="true">
                 <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self},Path=(Validation.Errors)[0].ErrorContent}"/>
              </Trigger>
           </Style.Triggers>
  </Style>

IDataErrorInfo实现的类
public class CurrencyExchangeRate : ObservableObject, IDataErrorInfo
{
    private string _code;
    public string Code
    {
        get { return _code; }
        set
        {
             if (_code != value)
            {
                _code = value;
                if (!string.IsNullOrEmpty(_code))
                {
                    RaisePropertyChangedEvent("Code");
                }
            }
        }
    }  

    public string this[string columnName]
    {
        get
        {
            string error = string.Empty;

            switch (columnName)
            {
                case "Code":

                    if (string.IsNullOrEmpty(_code))
                    {
                        error = "Code cannot be empty";
                        ShowMessage(error);
                    }
                    break;
            }
            return error;
        }
    }  
    public static void ShowMessage(string error)
    {
        System.Windows.Threading.Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
        {
            System.Windows.Forms.MessageBox.Show(error);
        }));
    }
}    

最佳答案

在实现IDataErrorInfo的类中,添加索引器MessageBox。为了在设置TextBox后仅显示一次

 <TextBox Text="{Binding Code, UpdateSourceTrigger=LostFocus}"/>

失去焦点后,它将提供检查验证。

关于c# - 如何在WPF文本 block 验证错误上显示消息框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25485071/

相关文章:

c# - 在 Entity Framework 中加入多个实体,2 个 DbSet 和一个列表

c# - 代码隐藏中的数据绑定(bind)

c# - 在 XAML 中绑定(bind)到自身/'this'

wpf - WPF 验证中的 ValidatesOnNotifyDataErrors 与 ValidatesOnDataErrors 和 NotifyOnValidationError 之间有什么区别?

c# - C#运行时动态创建Label的宽度

c# - 如何过滤数据 View

c# - 保留删除 Newtonsoft.Json 中的尾随零

WPF ResourceDictionary StringFormat XamlParseException

c# - 更改按钮控件模板,现在鼠标悬停不起作用

c# - 在 wpf 中制作 StackPanel 及其内容 "draggable"?