c# - 如何在wpf中的窗口中设置多个Datacontext

标签 c# wpf xaml mvvm datacontext

在应用程序中,我想绑定(bind)来自 2 个不同 View 模型的数据。我必须将数据上下文设置为两个不同的 View 模型。

当我使用两个不同的 View 模型时,只有最后设置的数据上下文优先。

因此,我有一个CombinedDataContext 类和其他 View 模型类。

我可以更新 View 模型,但它没有显示在 View 上。

View 模型1

public class Data1 : INotifyPropertyChanged
    {
        private string _string1;

        public string String1
        {
            get { return _string1; }
            set { _string1 = value; onPropertyChnaged("String1"); }
        }

        private void onPropertyChnaged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

    }

View 模型2

public class Data2 : INotifyPropertyChanged
    {
        private string _string2;

        public string String2
        {
            get { return _string2; }
            set { _string2 = value; onPropertyChnaged("String2");}
        }

        private void onPropertyChnaged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

    }

浏览次数:

public partial class MainWindow : Window
    {
        Data12 d12 = new Data12 { };
        public MainWindow()
        {
            InitializeComponent();
            DataContext = d12;
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            ViewModel_1();
            ViewModel_2();
        }

        private void ViewModel_1()
        {
            var viewmodel = DataContext as Data12;
            viewmodel.Data1.String1 = textBox.Text;
        }
        private void ViewModel_2()
        {
            var viewmodel = DataContext as Data12;
            viewmodel.Data1.String1 = textBox.Text;
        }
}

组合 View 模型:

class Data12
    {
        public Data1 Data1 = new Data1();
        public Data2 Data2= new Data2();
    }

Xaml 文件:

<Grid>

        <Label x:Name="label" HorizontalAlignment="Left" Margin="92,61,0,0" VerticalAlignment="Top" Content="{Binding Data1.String1}"  />

        <Label x:Name="label1" Content="{Binding Data2.String2}" HorizontalAlignment="Left" Margin="349,61,0,0" VerticalAlignment="Top" />
        <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="65,153,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
        <TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="23" Margin="360,153,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="205,246,0,0" VerticalAlignment="Top" Width="75" Click="button_Click" />
</Grid>

没有 Nullreference,但绑定(bind)不起作用。我什至尝试实例化内部属性,但没有成功。请帮我解决这个问题。

最佳答案

您无法绑定(bind)字段,因此只需将它们转换为属性即可:

class Data12
{
    public Data12()
    {
        this.Data1 = new Data1();
        this.Data2 = new Data2();
    }
    public Data1 Data1 { get; private set; }
    public Data2 Data2 { get; private set; }
}

正如 @Clemens 评论的那样,这可以缩短为

class Data12
{
    public Data1 Data1 { get; } = new Data1();
    public Data2 Data2 { get; } = new Data2();
}

关于c# - 如何在wpf中的窗口中设置多个Datacontext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47083213/

相关文章:

C# Windows 窗体代码不工作 - 将事件附加到按钮

c# - WPF 自定义 Chrome 窗口?

silverlight - XAML 到 XHTML 转换器

c# - 如何创建一个 UI 来模拟 WPF 中的 RAM 分区?

wpf - 在 WPF 中的单个 GeometryDrawing 中使用多个画笔

c# - XAML x :bind oneTime on initially null property still works. 为什么?

c# - 分页数学问题

c# - 如何在 ListView 中查找项目并更新同一行中的另一列?

c# - 使用字典键(作为字符串)在构造函数中设置变量

c# - 打开选定文件的默认应用程序(MSWord、Adobe 等)