0

On my MainWindow i have a textbox and it's text is binded to a MainClass which has a public string called "TBText" (has the propertychanged/raisepropertychanged on it). I also have a button that triggers an event on another 2 classes.

I have 2 other classes that both have another string set to say "ee" and when that button is pressed it fires the property changed.

In the MainClass, when a propertychanged event occours (INotifyPropertyChanged) on either of the classes, it updates that main string called "TBText".

When i click the button, the string from 1 class is set to the main string but not from the other class, meaning you only see the string from the first class and not the second (even though there is a propertychanged event going on in both classes)

I've tried using a task to sort of disconnect the UI from the actual code/business behind, but that doesn't work either.

I've atried setting the main string as a set string in the mainclass ("eeeeee") when either propertychanged events are fired, And it still only updates to the first class, not the second.

I've also tried setting the main string as the second class' string when the first class' propertychanged event fires... still only sets it as the first class' string, almost as if the second class' string is null or empty, when it isn't.

XAML (MainWindow):

<Window.DataContext>
    <local:MainViewModel/>
</Window.DataContext>
<TextBox Text="{Binding Stat, UpdateSourceTrigger=PropertyChanged}"/>

MainViewModel:

private string StatText;
public string Stat { get { return StatText; } set { StatText = value; RaisePropertyChanged("Stat"); } }

private void ClassOne_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
        if (e.PropertyName == "Stat") Stat += ClassOne.Stat;
}
    private void ClassTwo_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
        if (e.PropertyName == "StatsTwo") Stat += ClassTwo.StatsTwo;
}

I haven't included it in this, but when a button is pressed in the MainWindow, it's command is linked to the 2 classes and fires the same event (i've tested and they both fire at the same time). Only problem is that the first class only changes the text, second class doesnt.

And i also want to say i haven't use MVVM all that much, but im sort of comfortable with binding text/content, commands, etc.

Is there a way to fix this? because i have no idea why it doesnt work...

Edit: the class one's variable is changed by the MainWindow and the class two variable is changed by another window

REghZY
  • 69
  • 1
  • 7
  • so i did a little more testing and found this: the string that the textbox is binded to is called "Stat" (in xaml, Text="{Binding MainClass.Stat}") When the class one makes MainClass.Stat equal to say "hello", the textbox's text becomes "hello" when the second class makes MainClass.Stat equal to say "hellothere", it stays as "hello", which in my mind defies all logic tbh... – REghZY Mar 19 '19 at 22:28
  • Your explanation is very confusing and the sample does not demonstrate the problem. Please provide more details and sample code that demonstrates the problem – Shawn Kendrot Mar 19 '19 at 23:13
  • Your TextBox is bound to a view model? And this view model listens to the PropertyChanged event of two other classes? And the event of those two classes is raised simultaneously by a single button? Can you show the code of the class 'ClassTwo' and all related code (e.g. event subscriptions)? Difficult to tell the error in your code without the code. Make sure 'MainViewModel' subscribes properly to 'ClassTwo' and 'ClassTwo.StatsTwo' is initialized properly. Use _nameof()_ to avoid typos: _e.PropertyName == nameof(ClassTwo.StatsTwo)_ – BionicCode Mar 20 '19 at 06:48
  • Put a breakpoint into _ClassTwo_PropertyChanged_ event handler and check the values when running in debug. – BionicCode Mar 20 '19 at 06:53
  • _UpdateSourceTrigger=PropertyChanged_ in your TextBox's binding is only relevant when the Mode is OneWayToSource or TwoWay. Since you are not updating the binding source the property 'UpdateSourceTrigger' is ignored. – BionicCode Mar 20 '19 at 06:57
  • the code actually **should** work. i didnt mention but, the class one's variable is changed by the MainWindow and the class two variable is changed by another window. and after doing some more tests, it's something to do with the new window because when the second window's variable changes, it _can_ update the value (i used message boxes to test it) but it just doesnt want to. – REghZY Mar 20 '19 at 07:12

1 Answers1

0

It sounds like your views are binding to separate instances of the view model. Check out this stackoverflow post.

Bommarn
  • 69
  • 4
  • this seems to be the problem why... :) – REghZY Mar 20 '19 at 15:47
  • Hm, I think i might have misunderstood the last thing you wrote. I thought that your ClassOne_PropertyChanged was fired by the MainWindow and ClassTwo_PropertyChanged was fired from another window. Sorry for the confusion. – Bommarn Mar 20 '19 at 16:07