【问题标题】:WPF checkbox twoway binding from property to UI is not working on propertychanged event从属性到 UI 的 WPF 复选框双向绑定在 propertychanged 事件上不起作用
【发布时间】:2015-03-16 21:53:50
【问题描述】:

我在 Telerik Grid 视图控件的复选框数据列中使用双向绑定。当我更改 UI 上复选框的状态时,它可以正常触发属性更改事件。但我也希望反之亦然,在更改后面代码的属性值时,复选框状态也应该在 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>

后面的代码如下

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;

}

单击刷新按钮时,我希望取消选中所有复选框,但当我在 PopulateGridView 中将 ChangeValue 设置为 false 时,它​​们不会更新。请建议我怎样才能做到这一点。

【问题讨论】:

  • 如何将DataVal 设置为false
  • 我认为事件应该被称为PropertyChanged,而不是ApplyPropertyChanged。这甚至可以编译吗?

标签: c# wpf telerik-grid


【解决方案1】:

您需要指定何时更新 UI,在本例中为 onpropertychanged

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

【讨论】:

  • 感谢您的回复,但它现在仍在使用 UpdateSourceTrigger。
  • 属性更改事件处理程序的行为非常奇怪。如果我将处理程序名称从 ApplyPropertyChanged 更改为 PropertyChanged,那么即使不使用 UpdateSourceTrigger,它也会按预期工作。有谁知道为什么事件处理程序名称会影响双向绑定?
猜你喜欢
  • 2014-03-16
  • 1970-01-01
  • 2019-11-13
  • 2019-07-28
  • 2014-04-10
  • 2011-10-14
  • 1970-01-01
  • 2021-12-04
  • 2013-03-28
相关资源
最近更新 更多