4

我制作了一个自定义窗口,其中包含在资源字典中定义的模板。该窗口没有标题栏。

<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 propertyin myUserControl并将其绑定到label模板中的内容?

提前致谢!

问候 Loetn

4

2 回答 2

4

您可以这样做:给x:Name您的ContentPresenter并在绑定标签时参考它。

<Border CornerRadius="8,8,0,0"
   BorderBrush="{StaticResource BorderBrushColor}"
   Background="{StaticResource HeaderColor}"
   Grid.ColumnSpan="2"
   Grid.Row="0">
   <Label Content="{Binding Path=Content.(dsControls:WindowDetails.Title), ElementName="myPresenter"}" FontSize="20" />
</Border>
<ContentPresenter x:Name="myPresenter" Grid.Row="1" Grid.ColumnSpan="2" Content="{TemplateBinding Content}" />
于 2013-10-08T10:06:04.157 回答
3

尝试使用转换器

public class AttchedTitleToTitleConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return (value as FrameworkElement).GetValue(WindowDetails.TitleProperty);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

将其添加到您的ResourceDictionary

<local:AttchedTitleToTitleConverter x:Key="titleConverter"/>

在绑定中:

<Label Content="{Binding RelativeSource={RelativeSource Self}, Converter={DynamicResource titleConverter}}"/>

希望这可以帮助

于 2013-10-08T09:43:52.360 回答