0

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

guton
  • 43
  • 6

1 Answers1

0

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

punker76
  • 14,326
  • 5
  • 58
  • 96
  • @guton yes, i think, why not? – punker76 Apr 13 '12 at 21:04
  • I mean that this control is used by many view model if I cast for one of this all work ..but ...I dont know witch one... ...the control serve many view model (sorry for my english) – guton Apr 13 '12 at 21:07
  • ...in the code behind of control i don't know wich view model uses that control.... – guton Apr 13 '12 at 21:08
  • @guton try using a interface IYourModelView wiht the collection property MyCollection, look at my changed answer – punker76 Apr 13 '12 at 21:10
  • ...in my interface I ideally dont would have ever a collection ..i use ayende effetcus model see http://msdn.microsoft.com/en-us/magazine/ee819139.aspx – guton Apr 13 '12 at 21:24
  • I mean that I can have a modelview not based on a collection – guton Apr 13 '12 at 21:27