我为ContentControl
创建了ControlTemplate
,该项显示边界为TextBox
的背景可以着色。我创建了一个附加属性来保存一个属性,该属性定义是否显示背景。我似乎无法得到正确的语法绑定到模板中元素的Visibility
属性的附加属性。ControlTemplate绑定到附加的依赖项属性
的附加属性是:
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
如何设置绑定到属性?
首先,正确命名静态只读域:'公共静态只读的DependencyProperty BackgroundVisibleProperty = ... 。' –
好的,但附加的属性似乎对'ContentControl'有效:HasContentConverter中的一个断点成功中断,并且输出中没有消息。只有当在实际模板 –