将自定义依赖项属性绑定到View Model中的属性不起作用

时间:2018-04-02 10:27:39

标签: c# wpf data-binding

我在主窗口内有一个Dock面板。在Dock面板中,有一个继承自 UserControl 类的类的对象(比如object1)(实例化发生在XAML本身)。 object1具有另一个类的对象集合(比如object2),它继承自 Framework 元素。在object2中,有一个依赖属性,我需要将其绑定到主窗口视图模型中的属性。如何实现这一目标?

当我尝试绑定时,不会触发Dependency属性的SetValue和GetValue,即绑定没有发生。

MainWindow.xaml

<DockPanel x:Uid="DockPanel_3" Name="DockPanel_3" Grid.Column="1"
          Background="{DynamicResource LayoutManagerBackground}">
<local:LayoutManager x:Uid="m_LayoutManager" x:Name="m_LayoutManager"
          Margin="5"  Visibility="Collapsed">
<local:LayoutManager.Layouts x:Uid="m_Layouts">
<local:Layout x:Uid="local:Layout_1" LayoutName="{x:Static
          VisionUIConstants:LayoutNames.MeasurementSetup}"
          IsSelected="True" LayoutDescription="{StaticResource 
          TTDMeasurementView}" IconBrush="{Binding StitchIcon}">

布局类

public DrawingBrush IconBrush
    {
        get { return (DrawingBrush)GetValue(IconBrushProperty); }
        set { SetValue(IconBrushProperty, value); }
    }

    // Using a DependencyProperty as the backing store for IconBrush.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty IconBrushProperty =
        DependencyProperty.Register("IconBrush", typeof(DrawingBrush), typeof(Layout), new FrameworkPropertyMetadata()
        {
            PropertyChangedCallback = OnIconChanged,
            BindsTwoWayByDefault = true
        });

    private static void OnIconChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (e.OldValue != e.NewValue)
        {
            // code to be executed on value update
        }
    }

0 个答案:

没有答案