【发布时间】:2012-12-16 17:07:22
【问题描述】:
我正在尝试将数据网格的 selectedItem 与 MVVM 中的属性绑定。 问题是它不会触发属性的“Set”。
在我拥有的 xaml 部分:
<WPFCtrlDg:ExtDataGrid Name="_edgMessage" Grid.Row="1"
ItemsSource="{Binding Path=LNE_MESSAGE, Mode=OneWay}"
SelectedItem="{Binding Path=CurrentMessage, Mode=TwoWay}">
在代码部分:
private LNE_MESSAGE _currentMessage;
public LNE_MESSAGE CurrentMessage
{
get
{
if (_currentMessage == null)
{
ICollectionView collectionView = CollectionViewSource.GetDefaultView(LNE_MESSAGE);
if (collectionView != null)
return collectionView.CurrentItem as LNE_MESSAGE;
return null;
}
return _currentMessage;
}
set
{
ICollectionView collectionView = CollectionViewSource.GetDefaultView(LNE_MESSAGE);
if (collectionView != null)
collectionView.MoveCurrentTo(value);
_currentMessage = value;
OnPropertyChanged(() => CurrentMessage);
}
}
extdatagrid 是一个自定义控件,并且 selected item 属性是这样完成的:
public object SelectedItem
{
get { return GetValue(SelectedItemProperty); }
set { SetValue(SelectedItemProperty, value); }
}
public static readonly DependencyProperty SelectedItemProperty =
DependencyProperty.Register("SelectedItem", typeof(object), typeof(ExtDataGrid),
new UIPropertyMetadata((s, e) =>
{
ExtDataGrid extDg = s as ExtDataGrid;
Debug.Assert(extDg != null);
extDg.CurrentItem = e.NewValue;
}));
知道如何正确绑定 selecteditem 属性吗?
【问题讨论】:
标签: wpf mvvm binding datagrid selecteditem