DependencyProperty未更新

时间:2012-08-08 08:40:26

标签: c# wpf xaml data-binding dependency-properties

我有一个简单的WPF窗口,其中TabItem包含一个带颜色的ComboBox和一个自定义Canvas绘制一个带有该颜色的矩形。在我的PaintCanvas中,我有一个像这样的DependencyProperty:

class PaintCanvas : System.Windows.Controls.Canvas
{
    public static readonly DependencyProperty PaintObjectProperty = DependencyProperty.Register(
      "PaintObject", typeof(PaintObject), typeof(PaintCanvas), new PropertyMetadata(OnPaintObjectChanged));


    public PaintObject PaintObject
    {
        get { return this.GetValue(PaintObjectProperty) as PaintObject; }
        set 
        {
            this.SetValue(PaintObjectProperty, value);
        }
    }

    private static void OnPaintObjectChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        PaintCanvas canvas = (PaintCanvas)d;

        // Update stuff

        canvas.InvalidateVisual();
    }

    protected override void OnRender(DrawingContext dc)
    {
        base.OnRender(dc);

        if (PaintObject != null)
        {
            dc.DrawRectangle(new SolidColorBrush(PaintObject.Color), null, new Rect(0, 0, PaintObject.Width, PaintObject.Height));
        }
    }
}

PaintObject依赖项属性在xaml中绑定到PaintViewModel中的相应属性:

<TabControl>
    <TabItem DataContext="{Binding PaintViewModel}">
        <StackPanel >
            <ComboBox ItemsSource="{Binding Colors}" SelectedItem="{Binding Color}" />
            <my:PaintCanvas Width="100" Height="100" PaintObject="{Binding PaintObject}" />
        </StackPanel>
    </TabItem>
</TabControl>

PaintViewModel是Window的ViewModel中的一个属性:

class MainViewModel
{
    PaintViewModel paintViewModel;

    public MainViewModel()
    {
        paintViewModel = new PaintViewModel();
    }

    public PaintViewModel PaintViewModel
    {
        get { return paintViewModel; }
    }

    ...
}

实际的PaintViewModel:

class PaintViewModel : INotifyPropertyChanged
{
    PaintObject paintObject;
    ObservableCollection<Color> colors = new ObservableCollection<Color>();
    Color currentColor;

    public PaintObject PaintObject
    {
        get { return paintObject; }
        set { paintObject = value; RaisePropertyChanged("PaintObject"); }
    }

    public ObservableCollection<Color> Colors
    {
        get { return colors; }
    }

    public Color Color
    {
        get { return currentColor; }
        set { 
            currentColor = value; 
            RaisePropertyChanged("Color");
            paintObject.Color = currentColor;
            RaisePropertyChanged("PaintObject");
        }
    }

    // Constructors and INotifyPropertyChanged stuff... 
}

TabItem似乎正确绑定到视图模型,因为颜色组合框可以正常工作。但是,虽然更新了paint对象并调用了RaisePropertyChanged(“PaintObject”),但PaintCanvas中的DependencyProperty永远不会更新。我在这做错了什么?

1 个答案:

答案 0 :(得分:2)

我没有看到你更改对PaintObject的引用,你改变了它的一个属性(Color)并且当PaintObject被更改时触发,并且因为它不是,依赖属性不刷新

作为解决方案,您可以在PaintCanvas中添加Color依赖项属性,并将Color绑定到xaml中的PaintObject.Color

<my:PaintCanvas Width="100" Height="100" PaintObject="{Binding PaintObject}" Color={Binding PaintObject.Color} />

如果您不忘记在PaintObject中调用Color属性的NotifyPropertyChanged,将触发PaintConvas Color属性以进行更改

我看到你的设计有些混乱,试着保持简单