【问题标题】:CheckBox binded property doesn`t changeCheckBox 绑定属性不会改变
【发布时间】:2018-07-13 15:27:54
【问题描述】:

我有以下代码

Namespace WpfApplication1
{
    using System.ComponentModel;
    using System.Runtime.CompilerServices;

    using WpfApplication1.Annotations;
    using WpfApplication1.Enums;

    public class MainWindowViewModel : INotifyPropertyChanged
    {
        private bool _isItemEnabled;

        public MainWindowViewModel()
        {
            this.IsItemEnabled = false;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public bool IsItemEnabled
        {
            get
            {
                return this._isItemEnabled;
            }

            set
            {
                this._isItemEnabled = value;
                this.OnPropertyChanged(nameof(this.IsItemEnabled));
            }
        }


        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}



<CheckBox Grid.Row="0" Grid.Column="1" Margin="0,20" 
          IsChecked="{Binding Path = TimeIsEnabled, Mode=OneWay, UpdateSourceTrigger=PropertyChanged }"
          DataContext="{Binding ElementName = MainWindowViewModel}">
    TestIsEnabled
</CheckBox>

当我单击复选框时,位于文件后面代码上的属性 TimeIsEnabled 不会更改,并且它的断点也不会触发。我试图在视图模型中找到这个属性,但结果是一样的。请帮忙。

【问题讨论】:

    标签: c# .net wpf


    【解决方案1】:

    尝试将模式从 OneWay 更改为 TwoWay (Mode=TwoWay)。如果您有OneWay 绑定,则属性会更新用户界面,但用户界面不会更新属性。

        <CheckBox Grid.Row="0" Grid.Column="1" Margin="0,20" 
                  IsChecked="{Binding Path = TimeIsEnabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged }"
                  DataContext="{Binding ElementName = MainWindowViewModel}">
            TestIsEnabled
        </CheckBox>
    

    分析您发送的其余代码后,我发现了一些错误。首先,您绑定了错误的属性TimeIsEnabled 而不是IsItemEnabled。其次尝试像我在xaml 文件中那样构造DataContext下面:

    <Window x:Class="WpfApplication3.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApplication3"
            mc:Ignorable="d"
            Title="MainWindow" Height="350" Width="525">
        <Window.DataContext>
            <local:MainWindowViewModel />
        </Window.DataContext>
        <Grid>
            <CheckBox Grid.Row="0" Grid.Column="1" Margin="0,20" 
              IsChecked="{Binding IsItemEnabled,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged }">
                TestIsEnabled
            </CheckBox>
        </Grid>
    </Window>
    

    您的MainWindowViewModel 课程很好。我试过这个例子,它工作正常。

    【讨论】:

    • 问题出在数据上下文中
    • 但我仍然在输出窗口 System.Windows.Data 中出现错误错误:40:BindingExpression 路径错误:在“对象”“字符串”(HashCode=2067111156)上找不到“TimeIsEnabled”属性。绑定表达式:路径=TimeIsEnabled; DataItem='String' (HashCode=2067111156);目标元素是'CheckBox'(名称='');目标属性是“IsChecked”(类型“Nullable`1”)
    • 您是否在定义了 TimeIsEnabled 属性的类中实现了 INotifyPropertyChanged 接口。也许这个链接会对您的问题有所帮助:stackoverflow.com/questions/5285377/…
    • 关于您的错误也许您可以在此链接找到解决方案:stackoverflow.com/questions/33805067/…
    • 事实上,这是一个带有一个 xaml 文件和包含一个属性的视图模型的测试应用程序
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-24
    • 1970-01-01
    相关资源
    最近更新 更多