c# - 在 UserControl 中绑定(bind) Validation.ErrorTemplate

标签 c# wpf validation xaml dependency-properties

我有一个用户控件,它有一个文本框,其中它的文本属性绑定(bind)到一个名为 SelectedValue 的依赖属性。当用户输入文本时,该值会根据另一个名为 ItemsSource 的 DP 进行验证,以查看它是否在那里。如果没有,我会抛出一个错误。一切正常——当出现错误时,UC 中的 TB 周围有默认的红色框。

但我希望用户在创建 UC 实例时能够在 XAML 中指定一个 ControlTemplate。所以我想我可以创建另一个 ControlTemplate 类型的 DP,它们可以绑定(bind)到。这似乎可行,但我如何在 XAML 中实际实现它?如果它做类似的事情:

Validation.ErrorTemplate="{Binding ValidationTemplate}"

它会抛出一条错误消息“'ErrorTemplate' 属性不能被数据绑定(bind)。”。以下是代码的相关部分:

<Canvas DataContext="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}">
    ....
    <TextBox x:Name="ValueTextBox"
             TextWrapping="NoWrap" 
             GotFocus="_ValueTextBox_GotFocus"
             Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type Canvas}}, Path=ActualWidth}"
             Height="{Binding RelativeSource={RelativeSource AncestorType={x:Type Canvas}}, Path=ActualHeight}"
       ----->Validation.ErrorTemplate="{Binding ValidationTemplate}"<-----
             >

        <TextBox.Resources>
            <CollectionViewSource x:Key="UniqueNamesList" Source="{Binding ItemsSource}" />
        </TextBox.Resources>

        <TextBox.Text>
            <Binding Path="SelectedValue" >
                <Binding.ValidationRules>
                    <l:InListValidator ValidationStep="RawProposedValue" 
                                       IgnoreCase="True" 
                                       UniqueNames="{StaticResource UniqueNamesList}" />
                </Binding.ValidationRules>
            </Binding>
        </TextBox.Text>
    </TextBox>
    ....
</Canvas>

和 DP 本身:

public object ValidationTemplate
{
    get { return (ControlTemplate)GetValue(ValidationTemplateProperty); }
    set { SetValue(ValidationTemplateProperty, value); }
}
public static readonly DependencyProperty ValidationTemplateProperty =
    DependencyProperty.Register("ValidationTemplate"
                                , typeof(ControlTemplate)
                                , typeof(AutoCompleteComboBox)
                                , new FrameworkPropertyMetadata(new ControlTemplate()));

感谢任何帮助。

欧尼


更新:

谢谢大家。我实际上尝试了 Adi 和 Nit 的回应。两者都有效,但 Adi 更接近我所寻找的,而不必定义用户控件的本地模板。即使我实际上没有创建模板而只是添加绑定(bind),但设计人员给出了一个错误,Nit 也会实际运行。我确实需要稍微调整一下您的代码,以便将其设置在 TextBox 本身上:

public ControlTemplate ValidationTemplate
{
    get { return (ControlTemplate)GetValue(ValidationTemplateProperty); }
    set { SetValue(ValidationTemplateProperty, value); }
}
public static readonly DependencyProperty ValidationTemplateProperty =
    DependencyProperty.Register("ValidationTemplate"
                                , typeof(ControlTemplate)
                                , typeof(AutoCompleteComboBox)
                                , new FrameworkPropertyMetadata(new ControlTemplate(), OnValidationTemplateChanged));

private static void OnValidationTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    if (e.NewValue != null)
    {
        AutoCompleteComboBox control = (AutoCompleteComboBox)d;
        Validation.SetErrorTemplate(control.ValueTextBox, (ControlTemplate)e.NewValue);
    }
}

谢谢!

最佳答案

查看 Validation.ErrorTemplate MSDN page ,你可以看到它有 IsNotDataBindable元数据属性设置为 true,因此很遗憾,您无法将数据绑定(bind)到该属性。

我相信您仍然可以处理您的依赖属性的 OnChanged 事件,以使用 Validation.SetErrorTemplate() 自行设置该属性:

public static readonly DependencyProperty ValidationTemplateProperty =
    DependencyProperty.Register("ValidationTemplate",
                                typeof(ControlTemplate),
                                typeof(AutoCompleteComboBox),
                                new FrameworkPropertyMetadata(new ControlTemplate(), OnValidationTemplateChanged));

private static void OnValidationTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    Validation.SetErrorTemplate(d, (ControlTemplate)e.NewValue);
}

关于c# - 在 UserControl 中绑定(bind) Validation.ErrorTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19162714/

相关文章:

c# - 在给定数字的列表中找到最接近的较小值和较大值的最佳方法是什么

c# - 使用 WPF/MVVM 在运行时动态更改 UserControl 内容

WPF 绑定(bind) IsSelected 到 ViewModel 不会设置列表中未显示的项目

xml - 有效的 XML、有效的架构。我哪里出错了?

html - React JSX 文件的 W3C HTML 验证

Json 架构 "not in"枚举类型?

带有队列或列​​表的 C# 流畅接口(interface)

c# - 服务总线QueueClient.Receive如何工作?

c# - 如何在 C# 中使用 WMI 查询从 UWF 获取注册表和文件排除项

WPF - 在 DataTemplate 中使用 CroppedBitmap