首页 文章

从contentpresenter中的内容获取附加属性值

提问于
浏览
4

我创建了一个自定义窗口,其中包含在资源字典中定义的模板 . 窗口没有 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 propertyOwnerType

  • 到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 回答

  • 3

    这是你如何做到这一点:给你的 ContentPresenter x:Name 并在绑定标签时引用它 .

    <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}" />
    
  • 4

    尝试使用转换器

    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}}"/>
    

    希望这可以帮助

相关问题