C#,WPF绑定,MVVM,INotifyPropertyChanged,尝试绑定实例属性但未成功

C#,WPF绑定,MVVM,INotifyPropertyChanged,尝试绑定实例属性但未成功,c#,wpf,xaml,mvvm,inotifypropertychanged,C#,Wpf,Xaml,Mvvm,Inotifypropertychanged,有没有一种方法可以将一个类的实例放在另一个类中,然后用INotifyPropertyChanged更新它们的UI?例如,这些PlayerSeclass实例PlayerOne和PlayerTwo在WhiteJack类中,并在UI中更新它们?唯一有效的解决方案是将上下文直接设置到播放器的实例,然后将它们输入到主视图模型viewmodel中 class MultipleDataContexts { public WhiteJack WhiteJackViewModel {

有没有一种方法可以将一个类的实例放在另一个类中,然后用INotifyPropertyChanged更新它们的UI?例如,这些PlayerSeclass实例PlayerOne和PlayerTwo在WhiteJack类中,并在UI中更新它们?唯一有效的解决方案是将上下文直接设置到播放器的实例,然后将它们输入到主视图模型viewmodel中

   class MultipleDataContexts
    {
        public WhiteJack WhiteJackViewModel { get; set; }
        public PlayerBaseClass PlayerOne { get; set; }
        public PlayerBaseClass PlayerTwo { get; set; }
        public MultipleDataContexts()
        {
            PlayerOne = new PlayerBaseClass();
            PlayerTwo = new PlayerBaseClass();
            WhiteJackViewModel = new WhiteJack(PlayerOne, PlayerTwo);
        }
    }
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.DataContext = new MultipleDataContexts(); // set datacontext for the INotifyPropertyChanged
            InitializeComponent();
            this.WindowState = WindowState.Maximized; //Window to the max.
        }
}
我猜这是唯一可行的方法。视图必须直接查看设置的上下文,而不能查看其成员。我说得对吗?因为这不起作用:

this.DataContext = new WhiteJack();

是的,没有。我无法通过将文本块绑定到WhiteJack内的实例来更新UI,无论我是否将上下文设置为命名实例。

您正在将此(窗口)的DataContext设置为WhiteJack()的新实例。我仍然不确定您的最终目标是什么,但在MultipleDataContext类中实例化两个PlayerBaseClass对象将允许您使用创建的属性设置背景。因此,请在ViewModel中设置颜色:

模型

视图模型

        MultipleDataContexts mdc = new MultipleDataContexts();
        mdc.PlayerOne.TextBlock_Background = new SolidColorBrush(Colors.Red);
        mdc.PlayerTwo.TextBlock_Background = new SolidColorBrush(Colors.Black);
        this.DataContext = mdc;
XAML



设置这些属性并将MultipleDataContext类绑定到我的窗口会给我一个红色和黑色的文本块背景。

请在ViewModel中显示PlayerOne和PlayerTwo属性的代码。@MakerOfTheUnicake:请不要在注释中发布代码,而要,用添加的代码编辑您的问题。哪些代码不适合您?您是否试图用一个新的
PlayerBaseClass
实例替换其中一个玩家?因为如果是这样的话,那么那些
playerne
PlayerTwo
属性也应该引发
PropertyChanged
事件-换句话说,你的
WhiteJack
类也应该实现
INotifyPropertyChanged
。我试图将玩家的实例放在WhiteJack类中。这对我不起作用。也许它不起作用,因为我从未命名过实例。更新不起作用的内容。谢谢大家的帮助,我进一步完善了我的问题,把整个事情都清理干净了。谢谢。我不知道是什么改变了我自己的代码,但我有点复制你,它的工作。也许我没有在multipledatacontext中创建实例作为属性。是吗?如果是这样,那又有什么关系呢?为什么我不能做一些普通的例子呢?很难说。我不知道你的新WhiteJack()内发生了什么。可能在您认为/假设您正在设置属性TextBlock_Background时,您没有使用值设置属性TextBlock_Background?这与getter和setter有关。很有趣。当我移除getter和setter时。单步执行代码时,它会在xaml视图中正确显示值。但它从不更新用户界面。当我把它们加回去时,xaml被窃听了。但用户界面会更新,一切看起来都正常了。:)我认为绑定到字段会阻碍inotifyproperty更改以传播。不确定。
        MultipleDataContexts mdc = new MultipleDataContexts();
        mdc.PlayerOne.TextBlock_Background = new SolidColorBrush(Colors.Red);
        mdc.PlayerTwo.TextBlock_Background = new SolidColorBrush(Colors.Black);
        this.DataContext = mdc;
    <TextBlock x:Name="SeatOne_TextBlock" HorizontalAlignment="Left" Text="text" Background="{Binding PlayerOne.TextBlock_Background}" VerticalAlignment="Top"  Opacity="1" Height="155" Width="105" FontFamily="Courier New" Padding="0" Margin="0" />
    <TextBlock x:Name="SeatTwo_TextBlock" HorizontalAlignment="Left" Text="text" Background="{Binding PlayerTwo.TextBlock_Background}" VerticalAlignment="Top"  Opacity="1" Height="155" Width="105" FontFamily="Courier New" Padding="0" Margin="0" />