我创建了一个自定义窗口,其中包含在资源字典中定义的模板 . 窗口没有 Headers 栏 .
<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
中定义一个 Headers . 为了设置 Headers ,我做了一个 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(""));
}
我在_352642中设置 Headers 如下:
<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>
The problem
我无法将属性值绑定到 template
中的标签 .
What I already tried (and went wrong)
<Label Content="{Binding Path=(dsControls:WindowDetails.Title), RelativeSource={RelativeSource TemplatedParent}}"
FontSize="20" />
和
<Label Content="{Binding Path=(dsControls:WindowDetails.Title), RelativeSource={RelativeSource Self}}"
FontSize="20" />
另外,更改 Attached property
的 OwnerType
:
- 到WindowDetails
public static readonly DependencyProperty TitleProperty = DependencyProperty.RegisterAttached(“Title”,typeof(string),typeof(WindowDetails),new PropertyMetadata(“”));
当我这样做时,属性没有设置 . 但 label
的内容具有默认 property value
.
- To UserControl
public static readonly DependencyProperty TitleProperty = DependencyProperty.RegisterAttached(“Title”,typeof(string),typeof(UserControl),new PropertyMetadata(“”));
当我这样做时,属性已设置,但 label
的内容没有值 .
Question
如何在 UserControl
中设置 attached property
并将其绑定到模板中 label
的内容?
提前致谢!
问候Loetn
2 回答
这是你如何做到这一点:给你的
ContentPresenter
x:Name
并在绑定标签时引用它 .尝试使用转换器
将其添加到
ResourceDictionary
:在绑定中:
希望这可以帮助