ProductName という列を持つデータグリッドがあります。このセルテキストが変更されると、セル値を使用してフィルタリングすることにより、リストビューにいくつかの製品を入力します。したがって、ユーザーはリストビューから項目を選択でき、選択した製品はセルにマップされます。 IDataErrorInfo を使用して、選択した製品をデータベース テーブルに合わせて検証します。しかし、私の問題は現在、ユーザーがリストビューから有効な製品である製品を選択すると、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
}
データバックをデータグリッドにマップするリストビューイベント:
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;
}
}