[英]Binding ControlTemplate child to DependencyProperty on a custom control not working
[英]ControlTemplate Binding to Attached DependencyProperty
我正在为ContentControl
创建一个ControlTemplate
,它显示一个带有背景的有边框的TextBox
。 我创建了一个附加属性来保存一个属性,该属性定义是否显示背景。 我似乎无法将语法正确绑定到附加属性到模板中元素的Visibility
属性。
附属物是:
public static class AttachedPropertyExtensions
{
public static readonly DependencyProperty
BackgroundVisible = DependencyProperty.RegisterAttached(
"BackgroundVisible",
typeof(Visibility),
typeof(AttachedPropertyExtensions),
new PropertyMetadata(default(Visibility)));
public static void SetBackgroundVisible(UIElement element, Visibility value)
{
element.SetValue(BackgroundVisible, value);
}
public static Visibility GetBackgroundVisible(UIElement element)
{
return (Visibility)element.GetValue(BackgroundVisible);
}
}
ControlTemplate
:
<ControlTemplate x:Key="BorderedTextBlock" TargetType="ContentControl">
<Grid Margin="{StaticResource TextControlMarginThemeThickness}"
BorderBrush="{StaticResource TextBoxBorderThemeBrush}"
BorderThickness="{StaticResource TextControlBorderThemeThickness}">
<Border x:Name="backgroundBorder"
Background="{TemplateBinding Background}"
Visibility="{Binding Path=con:AttachedPropertiesExtensions.BackgroundVisible, RelativeSource={RelativeSource Mode=TemplatedParent}}" />
<ScrollViewer HorizontalScrollMode="Disabled"
VerticalScrollMode="Enabled"
VerticalScrollBarVisibility="Visible">
<ContentPresenter Height="80"
TextWrapping="Wrap"
Margin="{StaticResource TextControlThemePadding}" />
</ScrollViewer>
</Grid>
</ControlTemplate>
这些用于:
<UserControl ...
xmlns:con="using:Scanners.Tetra.UWPmvvm.Helpers">
...
<ContentControl x:Name="lblReturnText"
Template="{StaticResource BorderedTextBlock}"
Content="{Binding ReturnText}"
Background="#DDDDDD"
con:AttachedPropertyExtensions.BackgroundVisible="{Binding ReturnText, Converter={StaticResource HasContentConverter}}" />
<ContentControl x:Name="lblErrorText"
Template="{StaticResource BorderedTextBlock}"
Content="{Binding ErrorText}"
Background="#C03556"
con:AttachedPropertyExtensions.BackgroundVisible="{Binding ErrorText, Converter={StaticResource HasContentConverter}}" />
</UserControl>
HasContentConverter:
class HasContentConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
string val = (string)value;
string.IsNullOrWhiteSpace(val))
{
return Visibility.Visible;
}
else
{
return Visibility.Collapsed
}
...
运行应用程序(部署在ARM移动设备上)时,输出中会显示以下错误
Error: BindingExpression path error: 'con:AttachedPropertiesExtensions' property not found on 'Windows.UI.Xaml.Controls.ContentControl'. BindingExpression: Path='con:AttachedPropertiesExtensions.BackgroundVisible' DataItem='Windows.UI.Xaml.Controls.ContentControl'; target element is 'Windows.UI.Xaml.Controls.Border' (Name='backgroundBorder'); target property is 'Visibility' (type 'Visibility')
当我改变
Path=con:AttachedPropertiesExtensions.BackgroundVisible
至
Path=(con:AttachedPropertiesExtensions.BackgroundVisible)
(或带括号的任何东西)构建整个ControlTemplate
时出错:
The text associated with this error code could not be found.
E_UNKNOWN_ERROR
如何设置绑定到属性?
我无法在这里测试UWP,因为我们仍然使用Win7,但在WPF中,您的附加属性定义很好,包括名称。 看起来我错了那个部分。 我仍然只是为了遵循惯例而重命名,但WPF并不关心。 也许UWP会 - 我会确定。
对我来说,测试代码的原因只是绑定路径。 它需要围绕Path
parens,因为它是一个附加属性,并且它们具有不遵循C#属性命名约定的复杂名称:
Visibility="{Binding (con:AttachedPropertyExtensions.BackgroundVisible), RelativeSource={RelativeSource TemplatedParent}}"
Path
周围的parens告诉它整个字符串表示一个复杂的属性名称,所以它转到con:AttachedPropertyExtensions.BackgroundVisible
用于DependencyProperty
定义字段。 否则,它认为con:AttachedPropertyExtensions
应该是模板化父级的属性,它具有自己的属性,名为BackgroundVisible
。 但这甚至没有意义。 即使这不是语法错误:
public Visibility con:AttachedPropertyExtensions{ get; set; }
......这不是你在这里想要传达的东西。
如果我将PresentationTraceSources.TraceLevel=High
添加到原始Binding
,这是调试跟踪输出失败的部分:
System.Windows.Data警告:108:BindingExpression(hash = 4917414):在级别0 - 对于ContentControl.con:AttachedPropertyExtensions found accessor <null>
System.Windows.Data错误:40:BindingExpression路径错误:在'object'''ContentControl'(Name ='')'上找不到'con:AttachedPropertyExtensions'属性。 BindingExpression:路径= CON:AttachedPropertyExtensions.BackgroundVisibility; DataItem ='ContentControl'(Name =''); target元素是'Border'(Name =''); 目标属性是“可见性”(类型“可见性”)
System.Windows.Data警告:103:BindingExpression(hash = 4917414):用{NullDataItem}替换级别1的项目
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.