Álvaro García
Álvaro García

Reputation: 19396

Is it possible to bind a property of another user control?

I have two user control. The second one has a datagrid. I want the first user control to be notified when the selected item in the datagrid of the second user control is changed.

Is it possible bind properties of different user controls?

I was thinking about this:

<local:MyFirstUserControl MyPorperty={Binding ElementName MySecondUserControl, path=DataGrid.Selecteditem}/>
<local:MyFirstUserControl Name="MySecondUserControl"/>

Thanks.

Upvotes: -1

Views: 194

Answers (2)

Johnny Andrew
Johnny Andrew

Reputation: 109

You can use a messenger to send messages between view models (e.g DevExpress.Mvvm or MvvmLight)

https://docs.devexpress.com/WPF/17474/mvvm-framework/messenger

You can define and subscribe custom events. In your case, first user control would subscribe to some event (let's say event X) Second user control would publish event X with the corresponding object(selected item in your case). First user control will get notified each time selected item changed, get the data and do the processing.

Upvotes: -1

Reg Edit
Reg Edit

Reputation: 6924

You may control the interaction and data flow between two user controls in the page that uses them.

So when the selected item changes in the grid, raise an event. The parent page can handle it appropriately, in this case by calling a public method on the second user control.

Upvotes: 0

Related Questions