我有一个用户控件,其中的文本框的文本属性绑定到名为SelectedValue的依赖属性。当用户输入文本时,该值会与另一个名为ItemsSource的DP进行验证以查看其是否存在。如果不存在,则会抛出错误。一切正常-当有错误时,UC中的TB周围会有默认的红色框。
但是我希望用户在创建UC实例时能够指定XAML中的ControlTemplate。因此,我想创建另一个类型为ControlTemplate的DP,他们可以将其绑定到。这似乎有效,但我如何在XAML中实际实现它呢?如果我做类似以下的事情:
更新:
但是我希望用户在创建UC实例时能够指定XAML中的ControlTemplate。因此,我想创建另一个类型为ControlTemplate的DP,他们可以将其绑定到。这似乎有效,但我如何在XAML中实际实现它呢?如果我做类似以下的事情:
Validation.ErrorTemplate="{Binding ValidationTemplate}"
它抛出一个错误,显示“'ErrorTemplate'属性不能进行数据绑定”。以下是代码的相关部分:
<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()));
感谢任何帮助。
Ernie
更新:
谢谢大家。我实际上尝试了Adi和Nit的回答。两个都可以,但是Adi更接近我的要求,不需要为用户控件定义本地模板。Nit的代码实际上即使我没有实际创建模板并只添加绑定,也会运行,但设计器会出错。我必须稍微调整您的代码,Adi,以将其设置在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);
}
}
谢谢!