Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用MVVM将UserControl参数传递给ViewModel_C#_Wpf_Mvvm - Fatal编程技术网

C# 如何使用MVVM将UserControl参数传递给ViewModel

C# 如何使用MVVM将UserControl参数传递给ViewModel,c#,wpf,mvvm,C#,Wpf,Mvvm,我有一个UserControl,它从xaml接收一个参数,如下所示: <components:MyComponent Sex="MALE"/> <UserControl.DataContext> <components:MyComponentViewModel/> </UserControl.DataContext> public partial class MyComponent: UserControl

我有一个UserControl,它从xaml接收一个参数,如下所示:

  <components:MyComponent Sex="MALE"/>
<UserControl.DataContext>
    <components:MyComponentViewModel/>
</UserControl.DataContext>
 public partial class MyComponent: UserControl
 {
        public string Sex
        {
            get => (string)GetValue(SexParamProperty);
            set { SetValue(SexParamProperty, value); }
        }

        public static readonly DependencyProperty SexParamProperty = DependencyProperty.Register(nameof(Sex), typeof(string), typeof(MyComponent));

        public MyComponent()
        {
            InitializeComponent();
        }
 }
public class MyComponentViewModel: ViewModelBase
{
   public string Sex { get; set; }
}
MyComponentViewModel如下所示:

  <components:MyComponent Sex="MALE"/>
<UserControl.DataContext>
    <components:MyComponentViewModel/>
</UserControl.DataContext>
 public partial class MyComponent: UserControl
 {
        public string Sex
        {
            get => (string)GetValue(SexParamProperty);
            set { SetValue(SexParamProperty, value); }
        }

        public static readonly DependencyProperty SexParamProperty = DependencyProperty.Register(nameof(Sex), typeof(string), typeof(MyComponent));

        public MyComponent()
        {
            InitializeComponent();
        }
 }
public class MyComponentViewModel: ViewModelBase
{
   public string Sex { get; set; }
}

我想让ViewModel知道UserControl的Sex值是多少。这是针对MVVM模式还是有一种方法可以做到这一点?如何执行此操作?

为了将值从控件传递到视图模型,控件的属性通常是双向绑定的。可以声明依赖项属性,使其在默认情况下双向绑定:

public string Sex
{
    get => (string)GetValue(SexProperty);
    set => SetValue(SexProperty, value);
}

public static readonly DependencyProperty SexProperty =
    DependencyProperty.Register(
        nameof(Sex), typeof(string), typeof(MyComponent),
        new FrameworkPropertyMetadata(
            null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
请注意依赖项属性的标识符字段的约定:
属性


控件(无论是UserControl还是任何其他控件)也不能有自己的私有视图模型对象。相反,它将只公开“内部”用作控件XAML中绑定的源属性的可绑定属性,例如

<UserControl x:Class="MyNamspace.MyComponent" ...>
    <Grid>
        <TextBox
            Text="{Binding Sex,
                   RelativeSource={RelativeSource AncestorType=UserControl}}"/>
    </Grid>
</UserControl>

控件尤其不能设置自己的DataContext,因为绑定其属性

<components:MyComponent Sex="{Binding SexInViewModel}"/>

不会像预期的那样工作。源属性名称根据当前DataContext解析,当前DataContext将是控件的私有视图模型,而不是继承的DataContext中的(预期的)视图模型实例


还值得一提的是,像这样的控件没有特定视图模型类型(或一组属性)的依赖关系。因此,它提供了更好的可重用性。

命名错误。如果名为
Sex
DP的端口必须命名为
SexProperty
。如果DP命名为
SexParamProperty
,则该属性必须命名为
SexParam
。然后您可以绑定它
Sex=“{Binding SexInVM}”
,其中
SexInVM
是外部ViewModel的属性……它根本不需要
MyComponentViewModel