WPF data context scenario
-
31-05-2021 - |
Question
I have a user control in XAML code (a data grid) with this databind statement:
<WpfToolkit:DataGrid ItemsSource="{Binding Path=MyCollection}" x:Name="lvItems"
I use this user control in a presenter class where I istantiate a modelview class and set datacontext to an object in my view
...so MyCollection object is defined in may view class and not in the code behind of my control
but I Want to access this MyCollection property from codebehind because I want to implement a button event that filter my collection source
how can I access to MyCollection in codebehind or where i wrong...?
thanks
Solution
maybe something like that?
put this at your button click event or the button command
var yourModelView = this.DataContext as IYourModelView;
if (yourModelView != null) {
var yourColl = yourModelView.MyCollection;
// do something with this collection
}
EDIT
public IYourModelView
{
ICollection MyCollection {get; set;}
}
public class YourModelView1 : IYourModelView
{}
public class YourModelView2 : IYourModelView
{}
hope this helps
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow