0
votes

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

1
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
Your explanation is very confusing and the sample does not demonstrate the problem. Please provide more details and sample code that demonstrates the problemShawn Kendrot
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
Put a breakpoint into ClassTwo_PropertyChanged event handler and check the values when running in debug.BionicCode
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

1 Answers

0
votes

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