【问题标题】:Bound Property always returns false but WPF bound CheckBox is still checked绑定属性始终返回 false,但仍选中 WPF 绑定 CheckBox
【发布时间】:2010-12-02 06:51:45
【问题描述】:

我在 UserControl 中有一个复选框:

<CheckBox Content="Existing" IsChecked="{Binding Path=IsExistingTemplate, Mode=TwoWay}"/>

它绑定到具有属性 IsExistingTemplate 的 DataContext,该属性始终返回 False。 (在我的实际应用程序中,它并不总是返回 False!)。 DataContext 实现 INotifyPropertyChanged。

公共布尔值?是现有模板 { 得到 { 返回假; } 放 { OnPropertyChanged("IsExistingTemplate") } }

当用户点击 CheckBox 时,CheckBox 总是显示一个勾号。

如何强制 CheckBox 在用户单击时不显示勾号?

【问题讨论】:

  • 请发布您的 IsExistingTemplate 代码。

标签: wpf data-binding checkbox


【解决方案1】:

我记得有一个非常相似的问题,但我找不到在哪里以及如何解决它。我认为问题是由 WPF 如何更新绑定引起的,并且我认为我在某处读到它可以通过使用简单地传递值的 IValueConverter 来解决,但我已经对其进行了测试,但它似乎没有工作。它应该可以工作,因为使用转换器应该使绑定重新评估。

您总是可以处理 CheckBox 上的 Clicked 事件并通过代码更新值,但感觉很脏。抱歉,我无法提供更多帮助

<StackPanel.Resources>
    <local:PassThroughConverter x:Key="PassThroughConverter" />
</StackPanel.Resources>
<CheckBox IsChecked="{Binding IsExistingTemplate, Converter={StaticResource PassThroughConverter}}" Content="Stuff"/>

public class PassThroughConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value;
    }
}

【讨论】:

    猜你喜欢
    • 2016-04-26
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多