Wpf 如何将usercontrol的dependencyproperty绑定到usercontrol的viemodel属性?

Wpf 如何将usercontrol的dependencyproperty绑定到usercontrol的viemodel属性?,wpf,data-binding,binding,mvvm,mvvm-light,Wpf,Data Binding,Binding,Mvvm,Mvvm Light,我在usercontrol中有一个文本框和一个按钮,它在button Click中打开一个窗口,选择一些内容并放入文本框: 像这样: string myProperty; public string MyProperty { get { return myProperty; } set { myProperty= value;

我在usercontrol中有一个文本框和一个按钮,它在button Click中打开一个窗口,选择一些内容并放入文本框: 像这样:

  string myProperty;
    public string MyProperty
    {
        get
        {
            return myProperty;
        }
        set
        {
            myProperty= value;
            RaisePropertyChanged("MyProperty");
        }
    }

 public ICommand buttonClickCommand
 {
    get { return new RelayCommand(ShowItemView); }
 }
 void ShowItemView()
  {  
  var ItemViewModel = new ItemViewModel();
  var ItemView = new ItemView();
  ItemView .DataContext = ItemViewModel ;
  ItemView .ShowDialog();
  if (ItemViewModel .SelectedItem != null)
       myProperty= ItemViewModel .SelectedItem.Name;
  }
public static readonly DependencyProperty OperandTextValueProperty = DependencyProperty.Register("OperandTextValue",
           typeof(string),
           typeof(DefaultSpecItemControl),
            new FrameworkPropertyMetadata()
            {
                PropertyChangedCallback = OnOperandTextValue,
                BindsTwoWayByDefault = true
            });

    public string OperandTextValue
    {
        get
        {
            return (string)GetValue(OperandTextValueProperty);
        }
        set
        {
            SetValue(OperandTextValueProperty, value);

        }
    }
现在我想在一个窗口中创建usercontrol的两个实例,并将它们的文本框绑定到window的viewmodel的两个属性,因此我为其创建一个依赖属性,如下所示:

  string myProperty;
    public string MyProperty
    {
        get
        {
            return myProperty;
        }
        set
        {
            myProperty= value;
            RaisePropertyChanged("MyProperty");
        }
    }

 public ICommand buttonClickCommand
 {
    get { return new RelayCommand(ShowItemView); }
 }
 void ShowItemView()
  {  
  var ItemViewModel = new ItemViewModel();
  var ItemView = new ItemView();
  ItemView .DataContext = ItemViewModel ;
  ItemView .ShowDialog();
  if (ItemViewModel .SelectedItem != null)
       myProperty= ItemViewModel .SelectedItem.Name;
  }
public static readonly DependencyProperty OperandTextValueProperty = DependencyProperty.Register("OperandTextValue",
           typeof(string),
           typeof(DefaultSpecItemControl),
            new FrameworkPropertyMetadata()
            {
                PropertyChangedCallback = OnOperandTextValue,
                BindsTwoWayByDefault = true
            });

    public string OperandTextValue
    {
        get
        {
            return (string)GetValue(OperandTextValueProperty);
        }
        set
        {
            SetValue(OperandTextValueProperty, value);

        }
    }
并将其绑定到window的viewmodel中的属性,但我不知道如何将其绑定到usercontrol的viemodel,在本例中,将其绑定到控件的viewmodel中的MyProperty

1:有没有办法将usercontrol的dependencyProperty绑定到它自己的viewmodel

2:我走对路了吗


提前感谢。

是的,有一种方法可以将用户控件的依赖项属性绑定到它自己的视图模型,不过,在大多数情况下,您需要将用户控件的UI绑定到它自己,即

LayoutRoot.DataContext = this;

有关用户控件设计和绑定的一般指导,请参阅。

如果ItemView是一个能够取消/关闭的对话框,请使用下一行:if ItemView.ShowDialog==true if ItemViewModel.SelectedItem!=null myProperty=ItemViewModel.SelectedItem.Name;或者与此类似。为什么您认为必须在窗口中定义DependencyProperty,如果您遵循MVVM模式,那么最好在Windows的ViewModel中只定义简单的属性。然后,这些属性应该从相关UserControl的ViewModel返回值。你的问题我不清楚,请解释得更多更好。@Sam:我不是在窗口中定义dependencyProperty,我是在usercontrol中定义的。我想把它绑定到usercontrol的viewmodel。@stukselbax:我不明白你的意思!我想我的问题不清楚!这是因为我的英语不好D我在usercontrol中有一个名为OperationTextValue的依赖属性,在usercontrol的viewmodel中有一个名为Myproperty的属性,现在我想将它们绑定在一起!!因为我必须在usercontrol的viewmodel中设置该值。谢谢,但我不明白,请你解释一下,LayoutRoot是什么@Raha-查看我链接到的教程。LayoutRoot是用户控件的根元素。@ColinE,UserControl本身没有名为LayoutRoot的属性,我认为“LayoutRoot”是UserControl的默认模板使用的名称。