简体   繁体   English

如何在WPF中的窗口中设置多个Datacontext

[英]How to set Multiple Datacontext in a window in wpf

In the application I wanted to bind data from 2 different viewmodels. 在应用程序中,我想绑定来自2个不同视图模型的数据。 Which is I have to set datacontext to two different viewmodels. 我必须将datacontext设置为两个不同的视图模型。

When I did with two different viewmodels, only the last set datacontext is taking precedence. 当我使用两个不同的视图模型时,只有最后一组数据上下文优先。

Hence, I have a CombinedDataContext class with other viewmodel classes. 因此,我有一个CombinedDataContext类和其他viewmodel类。

I could able to update to viewmodel, but it is not getting displayed onto views. 我可以更新到viewmodel,但是它没有显示在视图上。

ViewModel1 ViewModel1

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;

    }

ViewModel2 ViewModel2

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;

    }

Views: 观看次数:

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;
        }
}

Combined ViewModel: 组合的ViewModel:

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

Xaml file: 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>

There is no Nullreference, but binding is not working. 没有Nullreference,但是绑定不起作用。 I tried even by instantiating inner properties, but did not work. 我什至通过实例化内部属性来尝试,但是没有用。 Please help me on this. 请帮我。

You can't bind on Fields, so just turn them into properties and it should work: 您无法在Fields上绑定,因此只需将它们转换为属性即可使用:

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

As @Clemens commented, this can be shortened to 正如@Clemens评论的那样,可以将其缩短为

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM