I have a class for managing selections in listboxes and the xam selection controls such as xamDataGrid. I am attempting to bind a selecteditems property of my class to the dataitem (a custom biz object) for the selected items in the xamDataGrid. Below is the code I am using which does not current seem to be working. WHAT is the correct way to bind to the dataitem of selected items in a xamDataGrid..
//presenter is a reference to an instance of xamDataGrid. My selection manager class is the target in this example.
Binding selecteditemsbinding = new Binding("SelectedItems.Records/DataItem"); selecteditemsbinding.Source = presenter; selecteditemsbinding.Mode = BindingMode.OneWay;
BindingOperations.SetBinding(target, SelectedItemsProperty, selecteditemsbinding);
Thanks!
I think the best way to approach this problem is using the behaviors, here is an example - XamDataGridBehavior :
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; using System.Windows; using System.Windows.Interactivity; using System.Windows.Data; using Infragistics.Windows.DataPresenter; using Infragistics.Windows.DataPresenter.Events; namespace Behaviors { public class XamDataGridBehavior : Behavior<XamDataGrid> { public readonly static DependencyProperty SelectedItemsProperty = DependencyProperty.Register("SelectedItems", typeof(IEnumerable), typeof(XamDataGridBehavior), new FrameworkPropertyMetadata() { BindsTwoWayByDefault = true, DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, }); public IEnumerable SelectedItems { get { return GetValue(SelectedItemsProperty) as IEnumerable; } set { SetValue(SelectedItemsProperty, value); } } public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register("SelectedItem", typeof(object), typeof(XamDataGridBehavior), new FrameworkPropertyMetadata() { BindsTwoWayByDefault = true, DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, }); public Object SelectedItem { get { return GetValue(SelectedItemProperty); } set { SetValue(SelectedItemProperty, value); } } protected override void OnAttached() { base.OnAttached(); AssociatedObject.SelectedItemsChanged += new OnSelectedItemsChanged; }
void OnSelectedItemsChanged(object sender, SelectedItemsChangedEventArgs e)
{
XamDataGrid dataGrid = (sender as XamDataGrid);
SelectedItems = (from item in dataGrid.SelectedItems select (item as DataRecord).DataItem).ToList();
if (dataGrid.SelectedItems.DataPresenter.ActiveDataItem != null)
SelectedItem = dataGrid.SelectedItems.DataPresenter.ActiveDataItem;
}
protected override void OnDetaching() { AssociatedObject.SelectedItemsChanged -= OnSelectedItemsChanged; base.OnDetaching(); } } }
XAML would be:
<i:Interaction.Behaviors> <be:XamDataGridBehavior SelectedItem="{Binding Path=VMSelectedItem}"/> </i:Interaction.Behaviors> Where VMSelectedItem is the viewmodel SelectItem you are interested in. That's pretty much it, cheers :) - Shams
<i:Interaction.Behaviors> <be:XamDataGridBehavior SelectedItem="{Binding Path=VMSelectedItem}"/> </i:Interaction.Behaviors>
Where VMSelectedItem is the viewmodel SelectItem you are interested in.
That's pretty much it, cheers :)
- Shams
You need to inherit from XamDataGrid and add a DependencyProperty used for Binding the seleted itemS:
private readonly static DependencyProperty SelectedProperty = DependencyProperty.Register("Selected", typeof(IEnumerable), typeof(DataGrid)); public IEnumerable Selected { get { return GetValue(SelectedProperty) as IEnumerable; } set { SetValue(SelectedProperty, value); } } public DataGrid() { SelectedItemsChanged += new EventHandler<SelectedItemsChangedEventArgs>(DataGrid_SelectedItemsChanged); } private void DataGrid_SelectedItemsChanged(object sender, SelectedItemsChangedEventArgs e) { List<object> selectedItems = new List<object>(); foreach (object selectedItem in base.SelectedItems) { if (selectedItem is Cell) selectedItems.Add(((selectedItem as Cell).Record as DataRecord).DataItem); else if (selectedItem is DataRecord) selectedItems.Add((selectedItem as DataRecord).DataItem); } Selected = selectedItems as IEnumerable; }
Then in Xaml you can do:
<controls:DataGrid DataSource="{Binding ...}" Selected="{Binding Selected, Mode=OneWayToSource}">
Refer to following URL :
http://help.infragistics.com/Help/NetAdvantage/WPF/2007.2/CLR3.X/html/xamDataGrid_Accessing_Cell_Values_in_the_Active_Record.html
Hope this will be usefull
Sohel
I've tried the activerecord property and that only returns a single, not several if you have multiple row selection enabled.