我爲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中的一個斷點成功中斷,並且輸出中沒有消息。只有當在實際模板 –