【发布时间】:2016-11-02 13:05:09
【问题描述】:
我正在为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
如何设置绑定到属性?
【问题讨论】:
-
首先,正确命名静态只读字段:
public static readonly DependencyProperty BackgroundVisibleProperty = .... -
好的,但是附加的属性似乎在
ContentControl上工作:HasContentConverter中的断点成功中断,并且从那时起输出中没有任何消息。仅在实际模板的边框上绑定到Visibility时使用