【问题标题】:WPF two way binding not working on CheckBoxWPF 双向绑定在 CheckBox 上不起作用
【发布时间】:2019-11-13 13:06:07
【问题描述】:

我无法使用CheckBox 进行双向绑定。

CheckBox 项目源是 PRESTAZIONI 并包含一个名为 Check_Statusbool 字段,默认值为 0。

数据绑定代码(DataClassesDataContext)是LINQtoSQL拖拽生成的,应该没问题。 它具有此解决方案 WPF CheckBox TwoWay Binding not working 的所有 ViewModel 部分。

我有这个 XAML 代码:

<StackPanel Grid.Column="3" Grid.Row="3">
    <ListBox Name ="listPrest" Height="91" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <CheckBox Name="chkPrest" Width="220" IsChecked="{Binding Path=Check_Status, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                    <TextBlock TextWrapping="Wrap">
                        <TextBlock.Text>
                            <MultiBinding StringFormat="{}{0} ({1})">
                                <Binding Path="Nome" />
                                <Binding Path="Code" />
                            </MultiBinding>
                        </TextBlock.Text>
                    </TextBlock>
                </CheckBox>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</StackPanel>

在视图的代码中,我像这样初始化CheckBoxes 的源代码:

public partial class InsertServiceView : UserControl
{
    DataClassesDataContext db = new DataClassesDataContext();
    IQueryable<PRESTAZIONI> prest = null;
    public InsertServiceView()
    {
        InitializeComponent();
        UpdatePrestList();
    }

    private void UpdatePrestList()
    {
        if (prest != null)
        {
            foreach (PRESTAZIONI p in prest)
            {
                p.Check_Status = false;
            }
        } 
        else 
        {
            prest = from p in db.PRESTAZIONI select p;
        }

        listPrest.ItemsSource = prest.ToList();
    }

    private void ButtonSave_Click(object sender, RoutedEventArgs e)
    {
        if (prest.Where(p => p.Check_Status == true).Count() == 0)
        {
            MessageBox.Show("No selections");
            return;
        }
    }
}

即使我最后检查了CheckBoxes,如果继续返回 true。

单向绑定曾经有效,但我需要将Check_Status 的值更新为所有false,因为我还以其他形式使用它,我希望它们都是false,当我打开这个时.

我看到了很多与我类似的问题并应用了所有解决方案,但仍然无法正常工作。 任何建议将不胜感激。 对不起英语不好。

编辑:

表 PRESTAZIONI 有一个主键。 这是拖放生成的 PRESTAZIONI 的代码:

protected virtual void SendPropertyChanged(String propertyName)
    {
        if ((this.PropertyChanged != null))
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }


protected virtual void SendPropertyChanging()
        {
            if ((this.PropertyChanging != null))
            {
                this.PropertyChanging(this, emptyChangingEventArgs);
            }
        }

public partial class PRESTAZIONI : INotifyPropertyChanging, INotifyPropertyChanged
{

    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

    private bool _Check_Status;

    partial void OnCheck_StatusChanging(bool value);
    partial void OnCheck_StatusChanged();

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Check_Status", DbType="Bit NOT NULL")]
    public bool Check_Status
    {
        get
        {
            return this._Check_Status;
        }
        set
        {
            if ((this._Check_Status != value))
            {
                this.OnCheck_StatusChanging(value);
                this.SendPropertyChanging();
                this._Check_Status = value;
                this.SendPropertyChanged("Check_Status");
                this.OnCheck_StatusChanged();
            }
        }
    }

【问题讨论】:

  • 什么是 PRESTAZIONI 类,Check_Status 属性是如何定义的?
  • 确认您的PRESTAZIONI 类实现了INotifyPropertyChanged 接口,并且在Check_Status 属性设置器已发送Check_Status 属性更改通知。
  • 不确定这是你的问题,但看看这个:stackoverflow.com/questions/1069227/…

标签: wpf checkbox binding


【解决方案1】:

我对@9​​87654321@ 不是很熟悉,但会不会是您的ToList() 调用正在生成PRESTAZIONI 的新实例?

在您的保存方法中,您正在检查原始的 IQueryable 实例。感觉你应该存储 List 并检查它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-10
    • 2011-10-14
    • 2013-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-14
    相关资源
    最近更新 更多