简体   繁体   English

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

[英]C#, WPF Binding, MVVM, INotifyPropertyChanged, Trying to bind instance properties without success

Is there a way to put instances of a class, inside another class, and then have the UI update from them with INotifyPropertyChanged? 有没有一种方法可以将一个类的实例放置在另一个类中,然后使用INotifyPropertyChanged从它们中更新UI? For example these PlayerBaseClass instances PlayerOne and PlayerTwo inside the WhiteJack class, and have them updated in the UI? 例如,WhiteJack类中的这些PlayerBaseClass实例PlayerOne和PlayerTwo,并在用户界面中对其进行了更新吗? The only solution that works is to have the context set directly to the instance of the players, and then feed them into the viewmodel which is the main view model..! 唯一有效的解决方案是将上下文直接设置为播放器的实例,然后将它们输入到作为主要视图模型的视图模型中。

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

Im guessing that this is the only way it will work. 我猜测这是它将起作用的唯一方法。 The View has to see directly into the set contexts, it cant see into its members. 视图必须直接查看设置的上下文,而不能查看其成员。 Am I right? 我对吗? Because this did not work: 因为这不起作用:

this.DataContext = new WhiteJack();

Yeah, no. 是的不 I cant get the UI to update with binding the textblocks to instances inside WhiteJack, no matter if I set the context to a named instance or not. 无论是否将上下文设置为命名实例,我都无法通过将文本块绑定到WhiteJack内的实例来更新UI。

You're setting the DataContext for this (the Window) to a new Instance of WhiteJack(). 您正在为此(窗口)将DataContext设置为WhiteJack()的新实例。 I'm still not sure exactly what your end goal is but instantiating your two PlayerBaseClass objects in your MultipleDataContexts class will allow you to set the Background using the properties you have created. 我仍然不确定您的最终目标是什么,但是在MultipleDataContexts类中实例化两个PlayerBaseClass对象将允许您使用创建的属性来设置Background。 So set your Color in your ViewModel: 因此,在ViewModel中设置您的颜色:

MODEL 模型

class MultipleDataContexts
{
    public PlayerBaseClass PlayerOne { get; set; }
    public PlayerBaseClass PlayerTwo { get; set; }
    public MultipleDataContexts()
    {
        PlayerOne = new PlayerBaseClass();
        PlayerTwo = new 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 XAML

    <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" />

Setting these properties and binding the MultipleDataContexts class to my Window gives me a red and a black textblock background. 设置这些属性并将并将MultipleDataContexts类绑定到我的Window,可以得到红色和黑色的文本块背景。

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

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