Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.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# UserControl数据上下文绑定_C#_Wpf_Mvvm_User Controls_Datacontext - Fatal编程技术网

C# UserControl数据上下文绑定

C# UserControl数据上下文绑定,c#,wpf,mvvm,user-controls,datacontext,C#,Wpf,Mvvm,User Controls,Datacontext,我的解决方案中有三个项目: 我的主WPF应用程序,其中包含MainWindow+MainViewModel 带有用户控件的用户控件库(ConfigEditorView) UIProcess类与UserControl(ConfigEditorViewModel)的ViewModel 在我的主窗口中,我想将UserControl与UIProcess的ViewModel一起使用 首先,我在主窗口中设置UserControl: 使用绑定到按钮的简单方法: private void doSomethi

我的解决方案中有三个项目:

  • 我的主WPF应用程序,其中包含MainWindow+MainViewModel
  • 带有用户控件的用户控件库(ConfigEditorView)
  • UIProcess类与UserControl(ConfigEditorViewModel)的ViewModel
在我的主窗口中,我想将UserControl与UIProcess的ViewModel一起使用

首先,我在主窗口中设置UserControl:

使用绑定到按钮的简单方法:

private void doSomething()
{
    ConfEditModel = new ConfigEditorViewModel("Hello World");
}
我的ConfigEditorViewModel基本上如下所示:

public class ConfigEditorViewModel : ViewModelBase
{
    private string _Description;
    public string Description
    {
        get
        {
            return _Description;
        }
        set
        {
            _Description = value;
            base.RaisePropertyChanged();
        }
    }

    public ConfigEditorViewModel(string t)
    {
        Description = t;
    }
}
说明已绑定到my UserControl中的文本框

<TextBox Grid.Row="1" Grid.Column="1" Margin="0,0,0,10" Text="{Binding Description}"/>

当我启动应用程序并单击按钮时,文本框应该包含“Hello World”,但它是空的

我做错了什么?

您的视图模型(以及可选的模型)需要实现

装订不是魔术。没有内置机制允许在普通旧属性的值更改时通知代码。您必须对其进行轮询,以检查是否发生了更改,这将是非常糟糕的,从性能角度来看

因此,绑定将查看它们所绑定的对象,并查看它们是否实现INotifyPropertyChanged,如果是,将订阅PropertyChanged事件。这样,当您更改属性并触发事件时,将通知绑定并更新UI

请注意,您必须实现该接口并正确使用它,但是它工作得很好。

您的视图模型(以及可选的模型)需要实现

装订不是魔术。没有内置机制允许在普通旧属性的值更改时通知代码。您必须对其进行轮询,以检查是否发生了更改,这将是非常糟糕的,从性能角度来看

因此,绑定将查看它们所绑定的对象,并查看它们是否实现INotifyPropertyChanged,如果是,将订阅PropertyChanged事件。这样,当您更改属性并触发事件时,将通知绑定并更新UI


请注意,您必须实现该接口并正确使用它,但是效果很好。

我给了你一个大致的答案:

在“real(一个你想与不同属性名的不同ViewModel一起使用的用户控件)”用户控件中,你可以绑定到你自己的从属属性,你可以使用ElementName或RelativeSource绑定来绑定,你应该不要在用户控件中设置数据上下文

 <UserControl x:Name="myRealUC" x:class="MyUserControl">
   <TextBox Text="{Binding ElementName=myRealUC, Path=MyOwnDPIDeclaredInMyUc, Path=TwoWay}"/>
 <UserControl>

我给了你一个大致的回答:

在“real(一个你想与不同属性名的不同ViewModel一起使用的用户控件)”用户控件中,你可以绑定到你自己的从属属性,你可以使用ElementName或RelativeSource绑定来绑定,你应该不要在用户控件中设置数据上下文

 <UserControl x:Name="myRealUC" x:class="MyUserControl">
   <TextBox Text="{Binding ElementName=myRealUC, Path=MyOwnDPIDeclaredInMyUc, Path=TwoWay}"/>
 <UserControl>

您的ConfEditModel没有通知UI更改,因此您的cel:ConfigEditorView的DataContext没有更新Hanks@nkoniishvt我在编码时没有考虑到这一点,但现在它工作正常。您的ConfEditModel没有通知UI更改,因此您的cel:ConfigEditorView的DataContext没有更新Hanks@nkoniishvt我没有考虑这在编码上很有用,但现在它工作得很好。
 <UserControl x:Name="myRealUC" x:class="MyUserControl">
   <TextBox Text="{Binding ElementName=myRealUC, Path=MyOwnDPIDeclaredInMyUc, Path=TwoWay}"/>
 <UserControl>
<myControls:MyUserControl MyOwnDPIDeclaredInMyUc="{Binding MyPropertyInMyViewmodel}"/>
    public readonly static DependencyProperty MyOwnDPIDeclaredInMyUcProperty = DependencyProperty.Register(
    "MyOwnDPIDeclaredInMyUc", typeof(string), typeof(MyUserControl), new PropertyMetadata(""));

    public bool MyOwnDPIDeclaredInMyUc
    {
        get { return (string)GetValue(MyOwnDPIDeclaredInMyUcProperty); }
        set { SetValue(MyOwnDPIDeclaredInMyUcProperty, value); }
    }