我制作了一个自定义窗口,其中包含在资源字典中定义的模板。该窗口没有标题栏。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dsControls="clr-namespace:Something">
<Style x:Key="WindowRegionStyle"
TargetType="Window">
/** Other setters **/
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ContentControl">
<Grid>
<Border Background="{StaticResource WindowBackground}"
BorderBrush="{StaticResource BorderBrushColor}"
BorderThickness="1"
CornerRadius="8"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Border CornerRadius="8,8,0,0"
BorderBrush="{StaticResource BorderBrushColor}"
Background="{StaticResource HeaderColor}"
Grid.ColumnSpan="2"
Grid.Row="0">
<Label Content="{Binding Path=(dsControls:WindowDetails.Title), RelativeSource={RelativeSource TemplatedParent}}"
FontSize="20" />
</Border>
<ContentPresenter Grid.Row="1" Grid.ColumnSpan="2" Content="{TemplateBinding Content}" />
</Grid>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
添加到此模板的内容是一个UserControl
. 这一切都有效。
但现在我想在UserControl
. 为了设置标题,我制作了一个attached property
'WindowDetails.Title'。
public static class WindowDetails
{
public static string GetTitle(DependencyObject obj)
{
return (string)obj.GetValue(TitleProperty);
}
public static void SetTitle(DependencyObject obj, string value)
{
obj.SetValue(TitleProperty, value);
}
// Using a DependencyProperty as the backing store for Title. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TitleProperty =
DependencyProperty.RegisterAttached("Title", typeof(string), typeof(WindowDetails), new PropertyMetadata(""));
}
我这样设置标题UserControl
:
<UserControl x:Class="Something.View"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dsControls="clr-namespace:Something"
Width="400"
dsControls:WindowDetails.Title="Test">
/** Some content **/
</UserControl>
问题
我无法将属性值绑定到我的template
.
我已经尝试过的(并且出错了)
<Label Content="{Binding Path=(dsControls:WindowDetails.Title), RelativeSource={RelativeSource TemplatedParent}}"
FontSize="20" />
和
<Label Content="{Binding Path=(dsControls:WindowDetails.Title), RelativeSource={RelativeSource Self}}"
FontSize="20" />
此外,更改OwnerType
:Attached property
To WindowDetails
public static readonly DependencyProperty TitleProperty = DependencyProperty.RegisterAttached("Title", typeof(string), typeof(WindowDetails), new PropertyMetadata(""));
当我这样做时,该属性未设置。但是 的内容label
有默认值property value
。
到用户控制
public static readonly DependencyProperty TitleProperty = DependencyProperty.RegisterAttached("Title", typeof(string), typeof(UserControl), new PropertyMetadata(""));
当我这样做时,属性被设置,但内容label
没有价值。
问题
如何设置attached property
in myUserControl
并将其绑定到label
模板中的内容?
提前致谢!
问候 Loetn