标签: errortemplate

WPF中的验证错误样式,类似于Silverlight

默认情况下,Validation.ErrorTemplateWPF只是一个小红色边框没有任何ToolTip.

Silverlight 4中,验证错误很好地开箱即用.

以下是Silverlight 4和WPF中出现的验证错误的比较

Silverlight 4
在此输入图像描述
WPF
在此输入图像描述

请注意WPF版本的平坦,无聊的外观与我认为的Silverlight外观相比.

WPF框架中是否存在任何类似的验证样式/模板,或者是否有人创建了很好的样式验证模板,如上面的Silverlight版本?或者我是否必须从头开始创建它们?

如果有人想尝试一下,可以使用以下代码重现上面的验证错误,适用于SilverlightWPF

主窗口/ MainPage.xaml中

<StackPanel Orientation="Horizontal" Margin="10" VerticalAlignment="Top">
    <TextBox Text="{Binding Path=TextProperty, Mode=TwoWay, ValidatesOnExceptions=True}"/>
    <Button Content="Tab To Me..." Margin="20,0,0,0"/>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)

主窗口/ MainPage.xaml.cs中

public MainWindow/MainPage()
{
    InitializeComponent();
    this.DataContext = this;
}

private string _textProperty;
public string TextProperty
{
    get { return _textProperty; }
    set
    {
        if (value.Length > 5)
        {
            throw new Exception("Too many characters");
        }
        _textProperty = value;
    } …
Run Code Online (Sandbox Code Playgroud)

c# silverlight wpf styles errortemplate

59
推荐指数
2
解决办法
3万
查看次数

带有验证的TextBox在选项卡更改时丢失ErrorTemplate

我有一个带有验证规则的TextBox,它位于TabControl的选项卡上.验证规则失败时,默认的ErrorTemplate正确显示(TextBox周围的红色边框).
但是,如果切换到另一个选项卡,然后使用TextBox返回选项卡,则ErrorTemplate hightlight将消失.如果TextBox中有更改,则仍会调用验证规则并返回false,但仍未显示错误突出显示.
只有当文本内容被更改为有效然后再次无效时才会重新亮相.
我希望如果文本内容无效,切换到另一个选项卡并返回保持无效突出显示.任何想要获得这种行为的想法都是最受欢
xaml:

<TextBox Height="35" >
  <TextBox.Text>
    <Binding Path="pan_id" UpdateSourceTrigger="PropertyChanged">
      <Binding.ValidationRules>
        <ps:PanIdValidation />
      </Binding.ValidationRules>
    </Binding>
  </TextBox.Text>
</TextBox>
Run Code Online (Sandbox Code Playgroud)

validation wpf errortemplate

32
推荐指数
3
解决办法
5967
查看次数

从ErrorTemplate访问Validation.Errors

我在网格中有一个BindingGroup:

<Grid x:Name="?????????????" DataContext="{Binding Source={StaticResource ????????}}"
    Grid.RowSpan="1" Grid.Row="1" HorizontalAlignment="Center">
  <Grid.BindingGroup>
    <BindingGroup NotifyOnValidationError="True">
      <BindingGroup.ValidationRules>
        <??:??????????? ValidationStep="ConvertedProposedValue" />
      </BindingGroup.ValidationRules>
    </BindingGroup>
  </Grid.BindingGroup>
  <Grid.Style>
    <Style>
      <Setter Property="Validation.ErrorTemplate" Value="{StaticResource BindingGroup??????}" />
    </Style>
  </Grid.Style>
  ...
Run Code Online (Sandbox Code Playgroud)

我的网格有一个ErrorTemplate:

<ControlTemplate x:Key="BindingGroup??????">
  <Border BorderBrush="Blue" BorderThickness="2">
    <StackPanel>
      <Label Content="My BindingGroup Error should be here!"></Label>
      <AdornedElementPlaceholder />
    </StackPanel>
  </Border>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)

我想从我的ControlTemplate访问Validation.Errors [0] .ErrorContent以在我的标签中显示它。可能吗?请你帮助我好吗?

validation wpf xaml controltemplate errortemplate

5
推荐指数
1
解决办法
477
查看次数

WPF 列表框错误模板

在我的 WPF 应用程序中,我有一个 ListBox 绑定到一组视图模型。这些视图模型通过实现 INotifyDataErrorInfo 来支持验证。我正在尝试为列表框中存在验证错误的项目显示错误模板。

通过在 ListBox的 ItemSource 绑定上设置NotifyOnValidationError=True,我能够让 ListBox 显示默认错误模板。
看起来像这样: 在此处输入图片说明

我的列表框代码:

<ListBox x:Name="ListBoxEvents" ItemsSource="{Binding Events, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}" 
                 IsSynchronizedWithCurrentItem="True" ItemTemplate="{DynamicResource EventListTemplate}"></ListBox>
Run Code Online (Sandbox Code Playgroud)

我的列表框样式:

<ControlTemplate x:Key="ListBoxValidationError">
    <DockPanel LastChildFill="True">
        <Border Background="Red" Margin="5">
            <AdornedElementPlaceholder />
        </Border>
    </DockPanel>
</ControlTemplate>

<Style TargetType="{x:Type ListBox}">
    <Setter Property="BorderBrush" Value="{StaticResource WindowTitleBrush}" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="MinWidth" Value="200" />
    <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
    <Setter Property="ScrollViewer.CanContentScroll" Value="False"></Setter>
    <Setter Property="Validation.ErrorTemplate" Value="{StaticResource ListBoxValidationError}"></Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

ListBox 项模板:

<DataTemplate x:Key="EventListTemplate" DataType="{x:Type event:EventViewModel}">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="Auto" /> …
Run Code Online (Sandbox Code Playgroud)

validation wpf xaml listbox errortemplate

5
推荐指数
1
解决办法
2400
查看次数

将 ControlTemplate 应用到窗口时错误模板不起作用

我有一个带有 App.xaml、MainWindow.xaml 和 Person 类的简单应用程序。当我不指定模板时,我的 ValidateOnDataErrors 工作正常,当文本框出错时,会在文本框周围放置红色边框。但是,一旦我在 MainWindow.xaml 的 Window 标记中插入“Template="{StaticResource WindowTemplate}"”,该字段仍然会被验证,但红色边框就会消失。

应用程序.xaml:

<Application x:Class="WpfPOC.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="MainWindow.xaml">
<Application.Resources>
    <ControlTemplate TargetType="Window" x:Key="WindowTemplate">
        <Grid Background="{TemplateBinding Background}">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Control x:Name="FocusCatcher"></Control>
            <TextBlock>Menu Section</TextBlock>
            <ContentPresenter Grid.Row="1" />
            <StatusBar Height="23" VerticalAlignment="Bottom" Grid.Row="2">
                <TextBlock Text="Current Editing Mode" />
            </StatusBar>
        </Grid>
    </ControlTemplate>
</Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)

主窗口.xaml:

<Window x:Class="WpfPOC.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:data="clr-namespace:WpfPOC"
    Template="{StaticResource WindowTemplate}"
    Title="MainWindow" 
    Height="350" 
    Width="525">
<Window.Resources>
    <data:Person x:Key="myDataSource" Name="Joe"/>
</Window.Resources>
<Grid>
    <TextBox Height="23" Width="120" Text="{Binding Source={StaticResource myDataSource}, …
Run Code Online (Sandbox Code Playgroud)

c# wpf wpf-controls controltemplate errortemplate

1
推荐指数
1
解决办法
1576
查看次数