【发布时间】:2015-04-02 02:24:09
【问题描述】:
我正在尝试让 WPF MVVM 模板与我在 WPF 但非 MVVM 应用程序中执行的基本功能一起工作。在这种情况下,我试图捕获 RowEditEnding 事件(我就是)来验证已更改行上的数据(这就是问题所在)。
在 XAML 中我使用了事件触发器:
<DataGrid AutoGenerateColumns="False" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch"
ItemsSource="{Binding oDoc.View}">
<DataGrid.Columns>
<DataGridTextColumn x:Name="docIDColumn" Binding="{Binding DocId}" Header="ID" Width="65"/>
<DataGridTextColumn x:Name="DocumentNumberColumn" Binding="{Binding Number}" Header="Document Number" Width="*"/>
<DataGridTextColumn x:Name="altIDColumn" Binding="{Binding AltID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="Alt" Width="55"/>
</DataGrid.Columns>
<i:Interaction.Triggers>
<i:EventTrigger EventName="RowEditEnding">
<i:InvokeCommandAction Command="{Binding DocRowEdit}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGrid>
使用委托命令路由到处理程序:
public ObservableCollection<Document> oDoc
{
get
{
return _oDoc;
}
}
public ICommand DocRowEdit
{
get { return new DelegateCommand(DocumentRowEditEvent); }
}
public void DocumentRowEditEvent()
{
//How do I find the changed item?
int i = 1;
}
我还没有找到方法来查找具有待处理更改的 ObservableCollection (oDoc) 成员。我注意到数据网格正在进行一些验证,如果我输入非数值,我想要更改的 AltID 字段将突出显示红色。但我想自己处理验证和相关的消息传递。我错过了什么?我正在考虑以某种方式引发属性更改事件,但没有找到如何将这样的东西连接到:
protected void RaisePropertyChangedEvent(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
最后两个代码块来自我的 ViewModel 类,除了在 MainWindow 构造函数中实例化 ViewModel 之外,我试图在没有任何代码的情况下执行此操作。
【问题讨论】: