2

我将 list<> 绑定到数据网格。然后,在后面的代码中,我更改了 list<> 中的值。如何在数据网格中显示更改?

Xml代码:

<DataGrid Name="gridTest" Grid.RowSpan="1" CanUserAddRows="False">
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="Enable" Width="SizeToHeader" IsReadOnly="False">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox  IsChecked="{Binding  Path=isEnable, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTextColumn Binding="{Binding  Path=test_name, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Header="test_name" Width="SizeToHeader"  IsReadOnly="True"/>
            <DataGridTextColumn Binding="{Binding  Path=test_type, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Header="test_type" Width="SizeToHeader" IsReadOnly="True"/>
            <DataGridTextColumn Binding="{Binding  Path=test_temp_index, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Header="test_temp_index" Width="SizeToHeader" IsReadOnly="True"/>
            <DataGridTextColumn Binding="{Binding  Path=lower_limit, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Header="lower_limit" Width="SizeToHeader" IsReadOnly="True"/>
            <DataGridTextColumn Binding="{Binding  Path=upper_limit, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Header="upper_limit" Width="SizeToHeader" IsReadOnly="True"/>
            <DataGridTextColumn Binding="{Binding  Path=board_test_point_description, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Header="board_test_point_description" Width="SizeToHeader" IsReadOnly="True"/>
            <DataGridTextColumn Binding="{Binding  Path=test_point_sequence, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Header="test_point_sequence" Width="SizeToHeader" IsReadOnly="True"/>
            <DataGridTextColumn Binding="{Binding  Path=fixture_test_point, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Header="fixture_test_point" Width="SizeToHeader" IsReadOnly="True"/>
        </DataGrid.Columns>
    </DataGrid>

代码背后:

列表:

public class fct_board_testpoint
{
    #region define Property
    public int profile_id { get; set; }
    public int test_point_sequence { get; set; }
    public int fixture_test_point { get; set; }
    public string test_type { get; set; }
    public float upper_limit { get; set; }
    public float lower_limit { get; set; }
    public string test_name { get; set; }
    public string board_test_point_description { get; set; }
    public int test_temp_index { get; set; }

    public bool isEnable { get; set; } //not in profile only for testing
    #endregion

}

捆绑:

this.gridTest.ItemsSource = UIBase.listTestPoint;

更改绑定源值:

foreach (fct_board_testpoint tp in UIBase.listTestPoint)
        {
            tp.board_test_point_description = "Success";
            tp.isEnable = true;
        }
4

1 回答 1

3

fct_board_testpoint 类必须实现 INotifyPropertyChanged,此接口保存一个事件,每次属性更改时都会引发该事件。

class fct_board_testpoint : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    bool isEnabled;

    public bool IsEnabled
    {
        get { return isEnabled; }
        set
        {
            isEnabled = value;
            PropertyChangedEventHandler temp = PropertyChanged;
            if (temp != null)
                temp(this, new PropertyChangedEventArgs("IsEnabled"));
        }
    }
}
于 2012-06-19T08:18:59.870 回答