WPF附加财产触发两次

时间:2017-10-04 11:29:28

标签: wpf c#-4.0 attached-properties

我正在尝试学习依赖属性和附加属性,所以如果你发现我没有尝试做什么,请原谅我。

我有一个通常的MVVM方法,其中一个Window的datacontext设置为一个VM,而View是一个包含一个用户控件的datatemplate,目标是这个VM。

我试图让窗口容器尽可能地变得愚蠢,因此我试图通过使用附加属性的用户控件来定义一些通常驻留在窗口XAML中的参数(例如高度)。

为此我创建了以下类,我在其中定义了附加属性:

public static class WpfExtensions
{
    public static readonly DependencyProperty ContainerHeightProperty = DependencyProperty.RegisterAttached(
      "ContainerHeight",
      typeof(double),
      typeof(WpfExtensions),
      new FrameworkPropertyMetadata(300.0, FrameworkPropertyMetadataOptions.AffectsParentArrange | FrameworkPropertyMetadataOptions.AffectsParentMeasure | FrameworkPropertyMetadataOptions.AffectsRender, ContainerHeight)
    );
    public static void SetContainerHeight(UIElement element, double value)
    {
        element.SetValue(ContainerHeightProperty, value);
    }
    public static double GetContainerHeight((UIElement element)
    {
        return (double)element.GetValue(ContainerHeightProperty);
    }

    private static void ContainerHeight(DependencyObject d, DependencyPropertyChangedEventArgs e)  
    {  
       if (d is UserControl)
       {
           UserControl l_Control = (UserControl)d;

           Binding l_Binding = new Binding();

           l_Binding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(Window), 1);

           l_Binding.Path = new PropertyPath("Height");

           l_Binding.Mode = BindingMode.OneWayToSource;

           BindingOperations.SetBinding(d, e.Property, l_Binding);           
       }
    }  
}

如您所见,为了实现容器窗口高度的控制,我在代码中创建了一个从附加属性到容器窗口的绑定。

然而,ContainerHeight更改被触发两次。我第一次从300(默认值)更改为1024(在XAML中定义的内容)。然后我立即收到另一个从1024回到300,我不明白为什么。

窗口代码非常简单:

<Window x:Class="WpfApplication.DialogWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:lcl="clr-namespace:WpfApplication"
        Title="DialogWindow">
    <Window.Resources>
        <DataTemplate DataType="{x:Type lcl:Dialog_VM}">
            <lcl:Dialog_VM_View />
        </DataTemplate>
    </Window.Resources>

    <ContentPresenter Content="{Binding }" />

</Window>

最后是简单的ViewModel

<UserControl x:Class="WpfApplication.Dialog_VM_View"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:lcl="clr-namespace:WpfApplication"
    mc:Ignorable="d" 
    d:DesignHeight="300" d:DesignWidth="300"
    lcl:WpfExtensions.ContainerHeight="1024">

    <StackPanel>
        <TextBlock Text="This is a test" />
    </StackPanel>

</UserControl>

1 个答案:

答案 0 :(得分:1)

您应该将父窗口的Height属性绑定到附加属性,而不是相反,不是吗?

这会将父窗口的Height设置(绑定)为1024,这是UserControl中依赖项属性的值:

private static void ContainerHeight(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    if (d is UserControl)
    {
        UserControl l_Control = (UserControl)d;
        if (!l_Control.IsLoaded)
        {
            l_Control.Loaded += L_Control_Loaded;
        }
        else
        {
            Bind(l_Control);
        }
    }
}

private static void L_Control_Loaded(object sender, RoutedEventArgs e)
{
    UserControl l_Control = (UserControl)sender;
    Bind(l_Control);
}

private static void Bind(UserControl l_Control)
{
    Window window = Window.GetWindow(l_Control);

    Binding l_Binding = new Binding();
    l_Binding.Path = new PropertyPath(WpfExtensions.ContainerHeightProperty);
    l_Binding.Source = l_Control;

    BindingOperations.SetBinding(window, Window.HeightProperty, l_Binding);
}