WPF 数据验证中的绑定失败

Posted

技术标签:

【中文标题】WPF 数据验证中的绑定失败【英文标题】:Failed Binding in WPF Data Validation 【发布时间】:2012-12-22 13:32:54 【问题描述】:

我正在尝试将我的控件样式中的属性绑定到控件中定义的属性。该控件继承自 TextBox,称为 ChangeTextBoxWithRangeUnits。我正在尝试从我创建的 ValidationRule 类绑定到它们。这是我尝试验证文本的设置器

 <Setter Property="Text">
        <Setter.Value>
            <Binding RelativeSource="RelativeSource Self"
                     Path="Text"
                     NotifyOnValidationError="True">
                <Binding.ValidationRules>
                    <basic:DoubleValidationRule>
                        <basic:DoubleValidationRule.MinMaxDependencyObject>
                            <basic:MinMaxDependencyObject Minimum="Binding RelativeSource=RelativeSource Mode=FindAncestor, AncestorType=x:Type local:ChangeTextBoxWithRangeUnits, Path=MinimumValue"
                                                          Maximum="Binding RelativeSource=RelativeSource Mode=FindAncestor, AncestorType=x:Type local:ChangeTextBoxWithRangeUnits, Path=MaximumValue" />
                        </basic:DoubleValidationRule.MinMaxDependencyObject>
                    </basic:DoubleValidationRule>
                </Binding.ValidationRules>
            </Binding>
        </Setter.Value>
    </Setter>

我不明白为什么它说找不到源绑定。这是错误:

System.Windows.Data 错误:4:找不到与引用“RelativeSource FindAncestor,AncestorType='Frasca.Simplicity.Controls.UnitControls.ChangeTextBoxWithRangeUnits',AncestorLevel='1''的绑定源。绑定表达式:路径=最小值;数据项=空;目标元素是“MinMaxDependencyObject”(HashCode=16320155);目标属性是“最小”(类型“双”)

编辑完整的 XAML

 <Style x:Key="ChangeTextBoxWithRangeUnits"
       TargetType="x:Type local:ChangeTextBoxWithRangeUnits"
       BasedOn="StaticResource x:Type TextBox">
    <Setter Property="Validation.ErrorTemplate"
            Value="x:Null" />
    <Setter Property="Height"
            Value="64" />
    <Setter Property="FontSize"
            Value="22" />
    <Setter Property="Margin"
            Value="5" />
    <Setter Property="Background"
            Value="LightGray" />
    <Setter Property="BorderThickness"
            Value="0" />
    <Setter Property="KeyboardNavigation.TabNavigation"
            Value="None" />
    <Setter Property="AllowDrop"
            Value="true" />
    <Setter Property="ScrollViewer.PanningMode"
            Value="VerticalFirst" />
    <Setter Property="Stylus.IsFlicksEnabled"
            Value="False" />
    <Setter Property="Text">
        <Setter.Value>
            <Binding RelativeSource="RelativeSource Self"
                     Path="Text"
                     NotifyOnValidationError="True">
                <Binding.ValidationRules>
                    <basic:DoubleValidationRule>
                        <basic:DoubleValidationRule.MinMaxDependencyObject>
                            <basic:MinMaxDependencyObject Minimum="Binding RelativeSource=RelativeSource Mode=FindAncestor, AncestorType=x:Type local:ChangeTextBoxWithRangeUnits, Path=MinimumValue"
                                                          Maximum="Binding RelativeSource=RelativeSource Mode=FindAncestor, AncestorType=x:Type local:ChangeTextBoxWithRangeUnits, Path=MaximumValue" />
                        </basic:DoubleValidationRule.MinMaxDependencyObject>
                    </basic:DoubleValidationRule>
                </Binding.ValidationRules>
            </Binding>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="x:Type local:ChangeTextBoxWithUnits">
                <Grid>
                    <Border Name="bg"
                            Background="TemplateBinding Background"
                            BorderBrush="TemplateBinding BorderBrush"
                            BorderThickness="TemplateBinding BorderThickness"
                            Width="TemplateBinding Width"
                            CornerRadius="15"
                            SnapsToDevicePixels="true">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="3*" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="2*" />
                            </Grid.RowDefinitions>

                            <StackPanel Orientation="Horizontal"
                                        Grid.Row="0"
                                        Name="mValueStackPanel"
                                        VerticalAlignment="Bottom"
                                        HorizontalAlignment="Center">
                                <ScrollViewer x:Name="PART_ContentHost"
                                              SnapsToDevicePixels="TemplateBinding SnapsToDevicePixels" />
                                <TextBox Name="PART_UnitTextBlock"
                                         Style="DynamicResource InlineTextBox"
                                         Text="Binding RelativeSource=RelativeSource FindAncestor, AncestorType=x:Type local:ChangeTextBoxWithUnits, Path=Units"
                                         FontSize="14"
                                         Margin="3,0,0,0"
                                         Foreground="TemplateBinding Foreground" />
                            </StackPanel>

                            <StackPanel Orientation="Horizontal"
                                        Grid.Row="2"
                                        HorizontalAlignment="Center"
                                        VerticalAlignment="Top">
                                <TextBlock Text="Binding RelativeSource=RelativeSource FindAncestor, AncestorType=x:Type local:ChangeTextBoxWithUnits, Path=Label"
                                           FontSize="14"
                                           Foreground="TemplateBinding Foreground" />
                            </StackPanel>

                            <TextBlock Text="Binding RelativeSource=RelativeSource FindAncestor, AncestorType=x:Type local:ChangeTextBoxWithUnits, Path=DisabledText"
                                       Foreground="TemplateBinding Foreground"
                                       Grid.Row="0"
                                       FontSize="14"
                                       HorizontalAlignment="Center"
                                       VerticalAlignment="Bottom"
                                       Visibility="Collapsed"
                                       x:Name="mDisabledTextBlock" />

                            <!-- Horizontal line -->
                            <Rectangle Height="1"
                                       Margin="10,0"
                                       Grid.Row="1"
                                       Opacity="0.15"
                                       SnapsToDevicePixels="True"
                                       Fill="TemplateBinding Foreground" />

                            <!-- Object which flashes when the textbox is selected -->
                            <Border Grid.RowSpan="3"
                                    Background="White"
                                    Name="FlashObject"
                                    CornerRadius="15"
                                    Opacity="0">
                                <Border.Effect>
                                    <BlurEffect Radius="20" />
                                </Border.Effect>
                            </Border>

                            <!-- Object which flashes when the textbox has a validation error-->
                            <Border Grid.RowSpan="3"
                                    Grid.ColumnSpan="2"
                                    Background="Red"
                                    Name="ErrorFlashObject"
                                    CornerRadius="15"
                                    Opacity="0">
                                <Border.Effect>
                                    <BlurEffect Radius="20" />
                                </Border.Effect>
                            </Border>
                        </Grid>
                    </Border>

                    <!-- Object which glows when the user makes a change to the text value. -->
                    <Border Width="Binding ElementName=bg, Path=ActualWidth"
                            Height="Binding ElementName=bg, Path=ActualHeight"
                            CornerRadius="15"
                            Background="#FFF066"
                            Name="ChangeGlowObject"
                            Panel.ZIndex="-1"
                            Visibility="Collapsed">
                        <Border.Effect>
                            <BlurEffect Radius="20" />
                        </Border.Effect>
                    </Border>
                </Grid>
                <ControlTemplate.Triggers>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="HasChangedValue"
                                       Value="True" />
                            <Condition Property="IsEnabled"
                                       Value="True" />
                        </MultiTrigger.Conditions>
                        <Setter Property="Visibility"
                                TargetName="ChangeGlowObject"
                                Value="Visible" />
                    </MultiTrigger>
                    <Trigger Property="IsEnabled"
                             Value="False">
                        <Setter Property="Background"
                                Value="#686868" />
                    </Trigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsEnabled"
                                       Value="False" />
                            <!--<Condition Property="ShowTextWhenDisabled"
                                       Value="False" />-->
                        </MultiTrigger.Conditions>
                        <Setter Property="Visibility"
                                TargetName="mDisabledTextBlock"
                                Value="Visible" />
                        <Setter Property="Visibility"
                                TargetName="mValueStackPanel"
                                Value="Collapsed" />
                    </MultiTrigger>

                    <!-- trigger to flash the object when the textbox has an error -->
                    <Trigger Property="Validation.HasError"
                             Value="True">
                        <Trigger.EnterActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation BeginTime="00:00:00"
                                                     Duration="00:00:00.2"
                                                     From="0"
                                                     To="1"
                                                     Storyboard.TargetName="ErrorFlashObject"
                                                     Storyboard.TargetProperty="Opacity">
                                        <DoubleAnimation.EasingFunction>
                                            <PowerEase Power="2"
                                                       EasingMode="EaseIn" />
                                        </DoubleAnimation.EasingFunction>
                                    </DoubleAnimation>
                                    <DoubleAnimation BeginTime="00:00:00.2"
                                                     Duration="00:00:00.5"
                                                     From="1"
                                                     To="0"
                                                     Storyboard.TargetName="ErrorFlashObject"
                                                     Storyboard.TargetProperty="Opacity">
                                        <DoubleAnimation.EasingFunction>
                                            <PowerEase Power="2"
                                                       EasingMode="EaseIn" />
                                        </DoubleAnimation.EasingFunction>
                                    </DoubleAnimation>
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.EnterActions>
                    </Trigger>

                    <!-- trigger to flash the object when the textbox is selected -->
                    <EventTrigger RoutedEvent="FocusManager.GotFocus">
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation BeginTime="00:00:00"
                                                 Duration="00:00:00.2"
                                                 From="0"
                                                 To="1"
                                                 Storyboard.TargetName="FlashObject"
                                                 Storyboard.TargetProperty="Opacity">
                                    <DoubleAnimation.EasingFunction>
                                        <PowerEase Power="2"
                                                   EasingMode="EaseIn" />
                                    </DoubleAnimation.EasingFunction>
                                </DoubleAnimation>
                                <DoubleAnimation BeginTime="00:00:00.2"
                                                 Duration="00:00:00.5"
                                                 From="1"
                                                 To="0"
                                                 Storyboard.TargetName="FlashObject"
                                                 Storyboard.TargetProperty="Opacity">
                                    <DoubleAnimation.EasingFunction>
                                        <PowerEase Power="2"
                                                   EasingMode="EaseIn" />
                                    </DoubleAnimation.EasingFunction>
                                </DoubleAnimation>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

【问题讨论】:

您能否添加一个完整的 XAML 示例? 【参考方案1】:

RelativeSource 绑定不会沿着您的 XML 前进,它们会沿着 Visual Tree 前进。这意味着您可以在您绑定的元素在该树中时使用它。

在这种情况下,您的DoubleValidationRule 不是 UIElement,并且它在可视化树中不是,因此RelativeSouce 在此规则上的绑定将失败。

不幸的是,在不知道您的DoubleValidationRule.MinMaxDependencyObject 的实现或目的的情况下,很难说您应该做什么。您可以通过更改验证规则的实现来绕过此限制。

一种可行的方法是,您可以通过 x:Name 值引用树中的元素

Minimum="Binding MinimumValue, ElementName=TheMinimumTarget

但这可能意味着您不能使用 Style 来执行此操作,因为每次使用都会(可能)绑定到表单上的不同元素。


查看您的编辑... Wat?您要将控件的文本绑定到控件的文本以附加验证规则吗?那是……我什至没有。

我建议对控件进行子类化以添加验证逻辑(例如,覆盖 Text 依赖属性的属性元数据),然后将您的模板应用于这种新类型。

【讨论】:

对,这是一种风格,所以我不能应用名称。 MinMaxDependencyObject 是一个简单的类,它继承具有最小值和最大值的 DependencyObject。它的唯一目的是让我可以对 DoubleValidationRule 使用绑定。你有什么建议吗?谢谢。

以上是关于WPF 数据验证中的绑定失败的主要内容,如果未能解决你的问题,请参考以下文章

WPF 数据绑定和验证规则最佳实践

2021-09-15 WPF上位机 16-属性绑定(数据验证)

WPF MVVM从入门到精通8:数据验证

利刃 MVVMLight 5:绑定在表单验证上的应用

WPF---数据绑定之ValidationRule数据校验

检测 WPF 验证错误