我有一个用户控件,它有一个文本框,其中它的文本属性绑定(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/