Guest User

Untitled

a guest
Jul 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. <ListView Name="MyListView">
  2. <ListView.View>
  3. <GridView>
  4. <GridViewColumn Header="Foo" DisplayMemberBinding="{Binding Foo}"/>
  5. <GridViewColumn Header="Bar" DisplayMemberBinding="{Binding Bar}"/>
  6. </GridView>
  7. </ListView.View>
  8. </ListView>
  9.  
  10. var foobars = new ObservableCollection<Foobar>();
  11. foobars.Add(new Foobar { Foo = "Hello", Bar = "world" });
  12. MyListView.ItemsSource = foobars;
  13.  
  14. var word = (string)(((ListViewItem)MyListView.Items[0]).SubItems[1]);
  15.  
  16. var word = ((Foobar)MyListView.Items[0]).Bar;
  17.  
  18. var columns = ((GridView) MyListView.View).Columns;
  19. var foobar = (Foobar)MyListView.Items[0];
  20. var result = (string)DataBinder.Eval(foobar, (Binding) columns[0].DisplayMemberBinding);
  21.  
  22. public class DataBinder
  23. {
  24. private static readonly DependencyProperty BindingEvalProperty = DependencyProperty.RegisterAttached(
  25. "BindingEval",
  26. typeof(Object),
  27. typeof(DependencyObject),
  28. new UIPropertyMetadata(null));
  29.  
  30. public static Object Eval(Object data, Binding binding)
  31. {
  32. var newbinding = new Binding {Path = binding.Path, Converter = binding.Converter, Source = data};
  33. var evalobj = new DependencyObject();
  34. BindingOperations.SetBinding(evalobj, BindingEvalProperty, newbinding);
  35. return evalobj.GetValue(BindingEvalProperty);
  36. }
  37. }
Add Comment
Please, Sign In to add comment