简体   繁体   中英

WPF checkbox twoway binding from property to UI is not working on propertychanged event

I am using twoway binding in a checkbox data column in Telerik Grid view control. when I change the state of checkbox on UI, it is working fine triggering the property changed event. But I also want vice versa, on changing the property value on code behind, the checkbox state should also update on UI.

<Button x:Name="btn1" Grid.Row="0" Content="Refresh" Click="btn1_Click" Width="100" Margin="0,5"/>
    <telerik:RadGridView Grid.Row="1"  x:Name="gridView" ShowGroupPanel="False" IsFilteringAllowed="False" SelectionMode="Multiple">
        <telerik:RadGridView.Columns>
            <telerik:GridViewDataColumn Width="70" Header="Color">
                <telerik:GridViewDataColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <CheckBox  IsChecked="{Binding ChangeValue, Mode=TwoWay}" Margin="3"/>
                        </StackPanel>
                    </DataTemplate>
                </telerik:GridViewDataColumn.CellTemplate>
            </telerik:GridViewDataColumn>
            <telerik:GridViewDataColumn Header="Data" DataMemberBinding="{Binding dataVal}" IsReadOnly="True"/>
        </telerik:RadGridView.Columns>
    </telerik:RadGridView>
</Grid>

The code behind is below

public partial class MainWindow : Window
{
    List<Data> dataSource = new List<Data>();
    public MainWindow()
    {
        InitializeComponent();            
        for (int i = 0; i < 10; i++)
        {
            data d = new Data();
            d.DataVal= "Data " + (i + 1);
            dataSource.Add(d);
        }
        this.gridView.SelectedItems.Clear();
        PopulateGridView();
    }
    private void PopulateGridView()
    {
        foreach (Data d in dataSource)
        {
            d.DataVal= false;
        }

        this.gridView.ItemsSource = dataSource;
        List<Data> selectedItems = new List<Data>();
        selectedItems.Add(dataSource[0]);
        this.gridView.Select(selectedItems);
    }

    private void btn1_Click(object sender, RoutedEventArgs e)
    {
        PopulateGridView();
    }

}

public class Data: INotifyPropertyChanged
{
    public Data()
    {
        DataVal = string.Empty;
    }
    public string DataVal { get; set; }

    public bool ChangeValue
    {
        get { return changevalue; }
        set
        {
            if (value != changevalue)
            {
                changevalue= value;
                if (ApplyPropertyChanged!= null)
                {
                    ApplyPropertyChanged(this, new PropertyChangedEventArgs("ChangeValue"));
                }
            }
        }
    }

    private bool changevalue;
    public event PropertyChangedEventHandler ApplyPropertyChanged;

}

On clicking Refresh button I want all checkbox unchecked but they are not updating when I am setting ChangeValue to false in PopulateGridView. Please suggest how can I achieve this.

您需要指定何时更新用户界面,在这种情况下应更改属性

       <CheckBox  IsChecked="{Binding ChangeValue, Mode=TwoWay, UpdateSourceTrigger="PropertyChanged"}" Margin="3"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM