WPF 与某些属性名称的绑定问题

发布于 2024-10-24 09:01:43 字数 1865 浏览 1 评论 0原文

我偶然发现了以下问题。我有一个复选框,其 IsChecked 属性绑定到我的 MainWindow 类中的 CLR 属性。这是源代码。

隐藏代码 (MainWindow.xaml.cs):

namespace MenuItemBindingTest {
  public partial class MainWindow : Window, INotifyPropertyChanged {
    private bool m_backedVariable = false;
    public bool IsPressAndHoldEnabled {
      get { return this.m_backedVariable; }
      set {
        this.m_backedVariable = value;
        OnPropertyChanged("IsPressAndHoldEnabled");
        MessageBox.Show("Item changed: " + this.m_backedVariable);
      }
    }

    public MainWindow() {
      InitializeComponent();
      this.m_checkbox.DataContext = this;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName) {
      if (this.PropertyChanged != null) {
        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
      }
    }
  }
}

XAML 代码 (MainWindow.xaml):

<Window x:Class="MenuItemBindingTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Binding Problem Test" Width="525" Height="350">
  <DockPanel>
    <CheckBox x:Name="m_checkbox" 
              IsChecked="{Binding IsPressAndHoldEnabled}"
              HorizontalAlignment="Center" VerticalAlignment="Center"
              Content="Is Press and Hold enabled"/>
  </DockPanel>
</Window>

现在的问题是属性 IsPressAndHoldEnabled 的 set 访问器> 当用户选中或取消选中复选框时,永远不会被调用(即,消息框永远不会显示)。但是,当我将属性重命名为其他名称时(例如 IsPressAndHoldEnabled2),它确实可以工作。

我现在的问题是:为什么我不能使用 IsPressAndHoldEnabled 作为我的属性的名称?这与现有的 Stylus.IsPressAndHoldEnabled 属性有什么关系吗?

I've stumbled over the following problem. I've got a checkbox whose IsChecked property is bound to a CLR property in my MainWindow class. Here's the source code.

Code behind (MainWindow.xaml.cs):

namespace MenuItemBindingTest {
  public partial class MainWindow : Window, INotifyPropertyChanged {
    private bool m_backedVariable = false;
    public bool IsPressAndHoldEnabled {
      get { return this.m_backedVariable; }
      set {
        this.m_backedVariable = value;
        OnPropertyChanged("IsPressAndHoldEnabled");
        MessageBox.Show("Item changed: " + this.m_backedVariable);
      }
    }

    public MainWindow() {
      InitializeComponent();
      this.m_checkbox.DataContext = this;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName) {
      if (this.PropertyChanged != null) {
        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
      }
    }
  }
}

XAML code (MainWindow.xaml):

<Window x:Class="MenuItemBindingTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Binding Problem Test" Width="525" Height="350">
  <DockPanel>
    <CheckBox x:Name="m_checkbox" 
              IsChecked="{Binding IsPressAndHoldEnabled}"
              HorizontalAlignment="Center" VerticalAlignment="Center"
              Content="Is Press and Hold enabled"/>
  </DockPanel>
</Window>

The problem now is that the set accessor for the property IsPressAndHoldEnabled is never called (ie. the message box never shows) when the user checks or unchecks the check box. It does, however, work when I rename the property to something else - like IsPressAndHoldEnabled2.

My question now is: Why can't I use IsPressAndHoldEnabled as name for my property? Does this have anything to do with the property Stylus.IsPressAndHoldEnabled existing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凯凯我们等你回来 2024-10-31 09:01:43

有趣的。我不知道为什么,但我有解决方法:

将 IsPressAndHoldEnabled 属性分离到单独的 ViewModel 类是有效的,除非该类是从 FrameworkElement 派生的。

此外,在同一个 MainWindow 类中从常规属性更改为依赖属性也有效 - DP 更改回调会触发。

Interesting. I don't have answers why, but I have workarounds:

Seperating the IsPressAndHoldEnabled property out to a seperate ViewModel class worked, unless the class was derived from FrameworkElement.

Also, changing from a regular property to a Dependency Property in the same MainWindow class worked -- the DP changed callback fires.

为你拒绝所有暧昧 2024-10-31 09:01:43

您是否已指定 TwoWay 作为绑定模式?虽然我认为 CheckBox.IsChecked 默认为 TwoWay 绑定模式...

我认为您可能搞乱了绑定上下文,因此它找不到 IsPressAndHoldEnabled 属性。 WPF 中的绑定会无提示失败——如果你问我的话,那真是太痛苦了。

检查该复选框是否确实绑定到该属性,并且绑定上下文是否确实是您的 MainWindodw 类对象。

Have you specified TwoWay as your binding mode? Although I think CheckBox.IsChecked defaults to TwoWay binding mode...

I think you may have messed up your binding context, so that it is not finding the IsPressAndHoldEnabled property. Bindings in WPF fail silently -- a royal pain if you ask me.

Check that the check-box is really bound to that property, and that the binding context really is your MainWindodw class object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文