为什么不能将viewmodel属性绑定到自定义控件的依赖项属性 [英] Why can't I Bind a viewmodel property to a dependency property of a custom control

查看:48
本文介绍了为什么不能将viewmodel属性绑定到自定义控件的依赖项属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在wpf应用程序中使用颜色选择器,并且在

I want to use a color picker in my wpf application and I saw a nice looking one on this codeproject page. The control works fine until I want to connect the control to a viewmodel. I created a small test program with this viewmodel:

public class ColorViewModel : ViewModelBase
{
    public ColorViewModel()
    {
        LineColor = Brushes.Yellow;
    }

    SolidColorBrush _brushColor;
    public SolidColorBrush LineColor
    {
        get { return _brushColor; }
        set
        {
            _brushColor = value;
            RaisePropertyChanged(() => LineColor);
        }
    }
}

测试程序具有一个文本框和颜色选择器控件:

The test program has a textbox and the colorpicker controls:

<StackPanel Orientation="Horizontal">
    <TextBlock Text="Please Select a Color" FontWeight="Bold" Margin="10"
               Foreground="{Binding Path=LineColor, UpdateSourceTrigger=PropertyChanged}"/>
     <vw:ColorPickerControlView x:Name="ForeColorPicker" Margin="10"
               CurrentColor="{Binding Path=LineColor, UpdateSourceTrigger=PropertyChanged }"/>
</StackPanel>

在测试应用程序的主窗口的加载事件中,将viewmodel设置为datacontext,如下所示:

In the loaded event of the main window in my test application I set the viewmodel to the datacontext like this:

 DataContext = new ColorViewModel();

问题是我似乎无法将viewmodel的LineColor属性绑定到ColorPickerControlView的CurrentColor属性.ColorPickerControlView的CurrentControl属性似乎很好.构造函数如下所示:

The problem is that I can't seem to bind the LineColor property of the viewmodel to the CurrentColor property of the ColorPickerControlView. The CurrentControl property of the ColorPickerControlView seems to be fine. The constructor looks like this:

public ColorPickerControlView()
{
    this.DataContext = this;
    InitializeComponent();
    CommandBindings.Add(new CommandBinding(SelectColorCommand, SelectColorCommandExecute));
}

在UserControl的构造函数中,有一行this.DataContext = this;我读到绑定依赖项属性是必需的.将视图模型设置为datacontext时,是否覆盖此行,这就是为什么我无法绑定到CurrentColor属性的原因?有什么解决方法吗?还是我犯了另一个错误?

In the constructor of the UserControl there is the line this.DataContext = this; I read that is is necessary to bind the dependency properties. Do I override this line when I set my viewmodel to the datacontext and is that why I can't bind to the CurrentColor property? Is there any workaround? Or did I make another mistake?

推荐答案

您正确地认为,如果不绑定到外部视图模型,则UserControl构造函数中的 DataContext = this 短语会优先.在此问题中进行了讨论.但是,这很容易补救.xaml绑定到的UserControl代码中只有一个DependencyProperty:CurrentColor.

这样做:

You are right in thinking that the DataContext=this phrase in the UserControl's constructor preempts if from binding to an external viewmodel. It was disccussed in this question. This is easily remedied however. There is only one DependencyProperty in the UserControl's code behind that the xaml binds to: CurrentColor.

Do this:

  • Name ="Root" 属性添加到UserControl的xaml
  • 更改(边界标签的)属性 Background ="{Path = CurrentColor}"到:

    Background ="{ElementName =根,Path = CurrentColor}"

  • 删除有问题的DataContext = this从UserControl的行构造函数!
  • Add a Name="Root" attribute to the UserControl tag of the UserControl's xaml
  • Change the attribute (of the Border tag) Background="{Binding Path=CurrentColor}" to:

    Background="{Binding ElementName=Root, Path=CurrentColor}"

  • Remove the offending DataContext=this line from the UserControl's constructor!

这应该就是它的全部了.我写了一个概念证明来证明上述内容.如果您愿意,我可以发布它,但是上面的代码应该就是您所需要的.

That should be all that there is to it. I wrote a proof of concept that demonstrates the above. If you like I can post it, but the code above should be all you need.

这篇关于为什么不能将viewmodel属性绑定到自定义控件的依赖项属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆