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>

問題

プロパティ値を my のラベルにバインドできません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

  • 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を設定してテンプレートUserControlのコンテンツにバインドするにはどうすればよいですか?label

前もって感謝します!

こんにちはロートン

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 に答える