Search code examples
c#xamlc#-4.0winui-3winui

How can I modify a UIElement.property from another class?


I'm working on a WinUI app (c# and xaml) with multiple frames and pages. The problem is that I need to modify a UIElement property (TextBox.Text) from another class. I've been trying so many things and none of them has worked yet. I'd be glad if someone could inspire me with some useful ways to do it. It can be anything aside from xaml data binding (<property={"Binding bindingName"}).

Thanks for the help.


Solution

  • Well, finally after two weeks of research I've found and check a solution.

    The solution is basically rising and catching events from the different classes. I found this information in a MSDN post. https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/events/how-to-publish-events-that-conform-to-net-framework-guidelines

    This post is REALLY STRAIGHT FORWARD. I've created the CustomEventArgs class and then the publisher class is my controller (.cs) and my subscriber is the model (.xaml.cs). The only problem that I've encountered is setting alll the methods and attributes as static in order to be able to call the controller function from other models. The problem is that on the raiseEvent(this, e); in the publisher class, this cannot be static so i've written null instead.

    Also, I'd like to say that it works extremely fluent and with no lag or delay at all even if I constantly use it.

    I hope this helps to everybody encountering the same problem.