1

我有一个带有名为 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
}

我的 listview 事件将数据映射回数据网格:

 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;
        }
    }
4

0 回答 0