1
votes

Is it possible to change the value of other object's DataContext when event trigger invoked using standard xaml tools? Thnx.

1

1 Answers

1
votes

I'm not sure what you are trying to achieve, but if you have multiple usercontrols that you want them to talk to each other and share data or invoke some sort of event. A good and clean way to do this without increasing the cohesion between the controls is to use a messaging system to publish and register to messages. This will allow you to trigger events and pass data between different controls. There are a few frameworks that support this.

You may want to look into Prism EventAggregator. The Prism library can be downloaded from the microsoft website.

Another framework is http://galasoft.ch/mvvm/. Look into the Messenger class.

If you want to change the other object's DataContext, the easiest way is to bind the object's datacontext to a property in your ModelView. Otherwise, you can just set the x:Name attribute for the object in XAML and reference that in the behind-code to change the datacontext to something different when the event is being invoked. This is assumed if you are within the same namescope/class.

*.XAML

<ComboBox x:Name="ComboBox1">
     <ComboBoxItem Content="Blah"/>
</ComboBox>

*.XAML.cs

public void SomeEventInvoked(object sender, EventArgs e) 
{
        ComboBox1.DataContext = someObject;
}