11

似乎我在此站点上阅读了有关此问题的另一个问题/答案,但我不记得答案是什么,现在我找不到原始帖子。

我不喜欢 WPF 中的默认错误模板。我了解如何更改此错误模板。但是,如果我在文本框的末尾添加一些内容,则文本框的大小不会改变,并且添加的内容将(可能)被剪裁。在这种情况下,如何更改文本框(我相信正确的术语是装饰元素),以便没有任何内容被剪裁?

这是错误模板的 XAML:

<Style TargetType="{x:Type TextBox}">
  <Setter Property="Validation.ErrorTemplate">
    <Setter.Value>
      <ControlTemplate>
        <StackPanel>
          <AdornedElementPlaceholder />
          <TextBlock Foreground="Red" Text="Error..." />
        </StackPanel>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

这是表单中几个文本框的 XAML:

<StackPanel>
  <TextBox Text="{Binding...}" />
  <TextBox />
</StackPanel>
4

1 回答 1

13

这是一个改编自 Josh Smith 的文章Binding to (Validation.Errors)[0] without Creating Debug Spew的解决方案。

诀窍是定义 aDataTemplate来呈现ValidationError对象,然后使用 aContentPresenter来显示错误消息。如果没有错误,则不会显示 ContentPresenter。

下面,我分享了我创建的示例应用程序的代码。

没有错误 有错误

这是 XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        SizeToContent="WidthAndHeight"
        Title="MainWindow">
    <StackPanel Margin="5">
        <StackPanel.Resources>
            <DataTemplate DataType="{x:Type ValidationError}">
                <TextBlock Text="{Binding ErrorContent}" Foreground="White" Background="Red" VerticalAlignment="Center" FontWeight="Bold"/>
            </DataTemplate>
        </StackPanel.Resources>
        <TextBox Name="TextBox1" Text="{Binding Text1, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"/>
        <ContentPresenter Content="{Binding ElementName= TextBox1, Path=(Validation.Errors).CurrentItem}" HorizontalAlignment="Left"/>

        <TextBox Name="TextBox2" Text="{Binding Text2, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"/>
        <ContentPresenter Content="{Binding ElementName= TextBox2, Path=(Validation.Errors).CurrentItem}" HorizontalAlignment="Left"/>
        <Button Content="Validate" Click="Button_Click"/>
    </StackPanel>
</Window>

文件背后的代码:

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private ViewModel _ViewModel = null;

        public MainWindow()
        {
            InitializeComponent();
            _ViewModel = new ViewModel();
            DataContext = _ViewModel;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            _ViewModel.Validate = true;
            _ViewModel.OnPropertyChanged("Text1");
            _ViewModel.OnPropertyChanged("Text2");
        }
    }
}

视图模型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace WpfApplication1
{
    public class ViewModel : INotifyPropertyChanged, IDataErrorInfo
    {
        private string _Text1;
        public string Text1
        {
            get { return _Text1; }
            set
            {
                _Text1 = value;
                OnPropertyChanged("Text1");
            }
        }

        private string _Text2;
        public string Text2
        {
            get { return _Text2; }
            set
            {
                _Text2 = value;
                OnPropertyChanged("Text2");
            }
        }

        public bool Validate { get; set; }

        #region INotifyPropertyChanged Implemenation
        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion

        #region IDataErrorInfo Implementation
        public string Error
        {
            get { return null; }
        }

        public string this[string columnName]
        {
            get
            {
                string errorMessage = string.Empty;
                if (Validate)
                {
                    switch (columnName)
                    {
                        case "Text1":
                            if (Text1 == null)
                                errorMessage = "Text1 is mandatory.";
                            else if (Text1.Trim() == string.Empty)
                                errorMessage = "Text1 is not valid.";
                            break;
                        case "Text2":
                            if (Text2 == null)
                                errorMessage = "Text2 is mandatory.";
                            else if (Text2.Trim() == string.Empty)
                                errorMessage = "Text2 is not valid.";
                            break;
                    }
                }
                return errorMessage;
            }
        }
        #endregion
    }
}
于 2012-10-03T13:33:39.297 回答