代码隐藏更改绑定源时更新UI

时间:2012-06-19 08:09:36

标签: wpf bind

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

Xaml代码:

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

1 个答案:

答案 0 :(得分: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"));
        }
    }
}