如何在控件模板中绑定自定义依赖项属性?

时间:2018-03-17 08:41:35

标签: wpf

我有一个类ChromeWindow,它派生自Window类。它有一个自定义依赖项属性

public class ChromeWindow : System.Windows.Window
    {
        public static readonly DependencyProperty TitleBarHeightProperty = DependencyProperty.Register("TitleBarHeight", typeof(int), typeof(ChromeWindow), new FrameworkPropertyMetadata(TitleBarHeightChangedCallback));

        public int TitleBarHeight
        {
            get
            {
                return (int)GetValue(TitleBarHeightProperty);
            }
            set
            {
                SetValue(TitleBarHeightProperty, value);
            }
        }

        private static void TitleBarHeightChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {

        }
    }

ChromeWindow有自己的控制模板,如下所示。

<ControlTemplate x:Key="ChromeWindowTemplate" TargetType="local:ChromeWindow">
            <Grid>
                        <AdornerDecorator>
                            <ContentPresenter />
                        </AdornerDecorator>

                        <StackPanel x:Name="PART_TitleBar"
                                    HorizontalAlignment="Stretch"
                                    VerticalAlignment="Top"
                                    Background="Transparent"
                                    Height="?">
                            <StackPanel HorizontalAlignment="Right"
                                        VerticalAlignment="Top"
                                        Orientation="Horizontal">
                                <Button x:Name="PART_MinimizeButton"
                                        Width="34"
                                        Height="26"
                                        Style="{DynamicResource SystemButton}"
                                        ToolTip="Minimize">

                                    <Path Width="8"
                                          Height="8"
                                          HorizontalAlignment="Center"
                                          VerticalAlignment="Center"
                                          Data="F1M0,6L0,9 9,9 9,6 0,6z"
                                          Fill="{Binding Foreground,
                                                         RelativeSource={RelativeSource Mode=FindAncestor,
                                                                                        AncestorType=Button}}" />

                                </Button>
                                <Button x:Name="PART_MaximizeButton"
                                        Width="34"
                                        Height="26"
                                        Style="{DynamicResource SystemButton}"
                                        ToolTip="Maximize">

                                    <Path x:Name="PART_MaximizeButtonPath"
                                          Width="10"
                                          Height="10"
                                          HorizontalAlignment="Center"
                                          VerticalAlignment="Center"
                                          Data="F1M0,0L0,9 9,9 9,0 0,0 0,3 8,3 8,8 1,8 1,3z"
                                          Fill="{Binding Foreground,
                                                         RelativeSource={RelativeSource Mode=FindAncestor,
                                                                                        AncestorType=Button}}" />
                                </Button>
                                <Button x:Name="PART_CloseButton"
                                        Width="34"
                                        Height="26"
                                        Style="{DynamicResource SystemButton}"
                                        ToolTip="Close">

                                    <Path Width="10"
                                          Height="8"
                                          HorizontalAlignment="Center"
                                          VerticalAlignment="Center"
                                          Data="F1M0,0L2,0 5,3 8,0 10,0 6,4 10,8 8,8 5,5 2,8 0,8 4,4 0,0z"
                                          Fill="{Binding Foreground,
                                                         RelativeSource={RelativeSource Mode=FindAncestor,
                                                                                        AncestorType=Button}}" />
                                </Button>
                            </StackPanel>
                        </StackPanel>

                        <ResizeGrip x:Name="PART_ResizeGrip"
                                    HorizontalAlignment="Right"
                                    VerticalAlignment="Bottom"
                                    Cursor="SizeNWSE"
                                    Visibility="Hidden" />
                    </Grid>
</ControlTemplate>

如何将 PART_Titlebar 高度绑定到名为“ TitleBarHeight ”的自定义依赖项属性,以便

  1. 如果未指定TitleBarHeight值,则标题栏高度应为
    根据其子项内容计算?

  2. 如果指定了TitleBarHeight值,请使用该值。

  3. 提前致谢。

1 个答案:

答案 0 :(得分:1)

Height属性的默认值为double.NaN,表示没有明确设置的高度。

因此,您应该将依赖项属性的类型更改为double并将double.NaN设置为其默认值:

public static readonly DependencyProperty TitleBarHeightProperty = 
    DependencyProperty.Register(
        nameof(TitleBarHeight), typeof(double), typeof(ChromeWindow),
        new FrameworkPropertyMetadata(double.NaN));

public double TitleBarHeight
{
    get { return (double)GetValue(TitleBarHeightProperty); }
    set { SetValue(TitleBarHeightProperty, value); }
}