我有一个带有ProductName列的数据网格,当这个单元格文本更改时,我通过使用单元格值过滤来填充一些产品的列表视图,因此用户可以从列表视图中选择一个项目,并将所选产品映射到cell.I使用IDataErrorInfo验证所选产品agianst我的数据库表。但我的问题是我设置UpdateSourceTrigger = LostFocus,当用户从列表视图中选择一个有效产品的产品时,数据网格仍显示验证错误。 ##标题## 我的xaml是:
<my:DataGrid Name="dgReceiveInventory" ItemsSource="{Binding}" SelectionUnit="Cell" AutoGenerateColumns="False" Margin="12,84,10,52" BeginningEdit="dgReceiveInventory_BeginningEdit">
<my:DataGrid.RowValidationRules>
<local:RowDataInfoValidationRule ValidationStep="UpdatedValue" />
</my:DataGrid.RowValidationRules>
<my:DataGrid.Columns>
<!--0-Product Column-->
<my:DataGridTemplateColumn Header="Product Name" Width="200">
<my:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding ProductName}" ></TextBlock>
<!--<TextBlock >
<TextBlock.Text>
<Binding Path="ProductName" >
</Binding>
</TextBlock.Text>
</TextBlock>-->
</DataTemplate>
</my:DataGridTemplateColumn.CellTemplate>
<my:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox x:Name="txtbxProduct" Text="{Binding Path=ProductName,Mode=TwoWay,ValidatesOnDataErrors=True}" Style="{StaticResource TextBoxInError}" TextChanged="txtbxProduct_TextChanged" PreviewKeyDown="txtbxProduct_PreviewKeyDown" >
</TextBox>
</DataTemplate>
</my:DataGridTemplateColumn.CellEditingTemplate>
</my:DataGridTemplateColumn>
</my:DataGrid.Columns>
</my:DataGrid>
<GridViewColumn DisplayMemberBinding="{Binding Path=Product_Name}" Header="Product" Width="200" />
</GridView>
</ListView.View>
</ListView>
我的目标是:
class clsProducts : INotifyPropertyChanged, IDataErrorInfo
{
private string _ProductName;
#region Property Getters and Setters
public string ProductName
{
get { return _ProductName; }
set
{
_ProductName = value;
OnPropertyChanged("ProductName");
}
}
#endregion
#region IDataErrorInfo Members
public string Error
{
get
{
StringBuilder error = new StringBuilder();
// iterate over all of the properties
// of this object - aggregating any validation errors
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(this);
foreach (PropertyDescriptor prop in props)
{
string propertyError = this[prop.Name];
if (!string.IsNullOrEmpty(propertyError))
{
error.Append((error.Length != 0 ? ", " : "") + propertyError);
}
}
return error.ToString();
}
}
public string this[string name]
{
get
{
string result = null;
if (name == "ProductName")
{
int count = Global.ItemExist(this._ProductName);
if (count == 0)
{
result = "Invalid Product";
}
}
return result;
}
}
#endregion
#region INotifyPropertyChanged Members
// Declare the event
public event PropertyChangedEventHandler PropertyChanged;
//// Create the OnPropertyChanged method to raise the event
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
#endregion
}
我的listview事件,它将数据包映射到datagrid:
private void lstvwProduct_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (lstvwProduct.SelectedItem != null)
{
int index = DataGridHelper.GetRowIndexOfSelectedCell(dgReceiveInventory);
if (index == -1 || index == dgReceiveInventory.Items.Count - 1)
return;
DataRowView drow = (DataRowView)lstvwProduct.SelectedItem;
lsItems[index].ProductName = drow.Row["Product_Name"].ToString();
lstvwProduct.Visibility = Visibility.Collapsed;
}
}