c# - 在 XAML 中将 bool 值返回 null 到复选框状态转换器

标签 c# xaml checkbox windows-store-apps win-universal-app

我有一个 TaskStatus 到 bool 值转换器,它在 Windows 应用商店应用程序(通用应用程序)的 XAML 中实现 IValueConverter 接口(interface)。

我有三个任务状态,并且我使用 IsThreeState="true"在复选框中启用了不确定状态。

现在,尽管 IsChecked 属性似乎是 bool 值?,转换器始终将 System.Boolean 作为目标类型。无论我返回什么(例如 null),总是会转换为 false,因此我无法在复选框中获得第三种状态。

有没有办法在我的转换器中指定 TargetType 或返回 null,以便 IsChecked 将 null 作为输入,从而显示第三种状态?

这是转换器:

public class TaskStatusToCheckBoxStateConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        var taskStatus = (TaskStatus) value;
        switch (taskStatus)
        {
            case TaskStatus.Open:
                return false;
            case TaskStatus.InProgress:
                return null;
            case TaskStatus.Done:
                return true;
            default:
                throw new ArgumentOutOfRangeException();
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        var checkBoxState = (Boolean?) value;
        if (checkBoxState == null)
            return TaskStatus.InProgress;
        if (checkBoxState.Value)
            return TaskStatus.Done;
        return TaskStatus.Open;
    }
}

复选框的 XAML 代码

<CheckBox x:Name="CheckBoxTaskState" 
    IsThreeState="True"
    IsChecked="{Binding Status, 
                Converter={StaticResource TaskStatusToCheckBoxStateConverter}, 
                Mode=TwoWay}">
</CheckBox>

最佳答案

根据 [this][1]:目前不支持在 WinRT 中绑定(bind)到可为 null 的类型。没有记录的规则怎么样?现在你知道了。

从这里开始

public sealed partial class MainPage : Page, INotifyPropertyChanged
{
    public MainPage()
    {
        this.InitializeComponent();
        this.DataContext = this;
    }

    private void NullButton_Click(object sender, RoutedEventArgs e)
    { this.State = null; }

    private void FalseButton_Click(object sender, RoutedEventArgs e)
    { this.State = false; }

    private void TrueButton_Click(object sender, RoutedEventArgs e)
    { this.State = true; }

    bool? _State = null;
    public bool? State { get { return _State; } set { SetProperty(ref _State, value); } }

    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
    void SetProperty<T>(ref T storage, T value, [System.Runtime.CompilerServices.CallerMemberName] String propertyName = null)
    {
        if (!object.Equals(storage, value))
        {
            storage = value;
            if (PropertyChanged != null)
                PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }
}

还有这个

<Grid x:Name="grid" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
        <StackPanel Orientation="Horizontal">
            <Button Click="TrueButton_Click" Content="True" />
            <Button Click="FalseButton_Click" Content="False" />
            <Button Click="NullButton_Click" Content="Null" />
        </StackPanel>
        <TextBlock Text="{Binding State}" />
        <CheckBox x:Name="checkBox"
                  Content="Hello three-state"
                  IsThreeState="True"
                  IsChecked="{Binding State, Mode=TwoWay}" />
    </StackPanel>
</Grid>

您可以在“输出”窗口中验证此错误。它的内容是这样的:

Error: Converter failed to convert value of type 'Boolean' to type 'IReference1<Boolean>'; BindingExpression: Path='State' DataItem='App4.MainPage'; target element is 'Windows.UI.Xaml.Controls.CheckBox' (Name='checkBox'); target property is 'IsChecked' (type 'IReference1').

我对此并不满意。因此,让我们使用附加属性来解决这个问题。

public class NullableCheckbox : DependencyObject
{
    public static bool GetEnabled(DependencyObject obj)
    { return (bool)obj.GetValue(EnabledProperty); }
    public static void SetEnabled(DependencyObject obj, bool value)
    { obj.SetValue(EnabledProperty, value); }
    public static readonly DependencyProperty EnabledProperty =
        DependencyProperty.RegisterAttached("Enabled", typeof(bool), typeof(NullableCheckbox), new PropertyMetadata(false, EnabledChanged));
    private static void EnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var checkbox = d as CheckBox;
        if ((bool)e.NewValue)
        {
            var binding = new Binding
            {
                Path = new PropertyPath("IsChecked"),
                Mode = BindingMode.TwoWay,
                Source = checkbox,
            };
            checkbox.SetBinding(NullableCheckbox.InternalStateProperty, binding);
        }
    }

    private static object GetInternalState(DependencyObject obj)
    { return (object)obj.GetValue(InternalStateProperty); }
    private static void SetInternalState(DependencyObject obj, object value)
    { obj.SetValue(InternalStateProperty, value); }
    private static readonly DependencyProperty InternalStateProperty =
        DependencyProperty.RegisterAttached("InternalState", typeof(object),
        typeof(NullableCheckbox), new PropertyMetadata(null, InternalStateChanged));
    private static void InternalStateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    { SetIsChecked(d, (object)e.NewValue); }

    public static object GetIsChecked(DependencyObject obj)
    { return (object)obj.GetValue(IsCheckedProperty); }
    public static void SetIsChecked(DependencyObject obj, object value)
    { obj.SetValue(IsCheckedProperty, value); }
    public static readonly DependencyProperty IsCheckedProperty =
        DependencyProperty.RegisterAttached("IsChecked", typeof(object),
        typeof(NullableCheckbox), new PropertyMetadata(default(object), IsCheckedChanged));
    private static void IsCheckedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var checkbox = d as CheckBox;
        bool? newvalue = null;
        if (e.NewValue is bool?)
            newvalue = (bool?)e.NewValue;
        else if (e.NewValue != null)
        {
            bool newbool;
            if (!bool.TryParse(e.NewValue.ToString(), out newbool))
                return;
            newvalue = newbool;
        }
        if (!checkbox.IsChecked.Equals(newvalue))
            checkbox.IsChecked = newvalue;
    }
}

你的 XAML 只会像这样改变:

<CheckBox Content="Hello three-state"
          IsThreeState="True"
          local:NullableCheckbox.Enabled="true"
          local:NullableCheckbox.IsChecked="{Binding State, Mode=TwoWay}" />

常规的 IsChecked 属性并不重要,它会被附加属性覆盖。您的 View 模型可以保持不变。这真的很神奇,不是吗?

祝你好运!

关于c# - 在 XAML 中将 bool 值返回 null 到复选框状态转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23860522/

相关文章:

c# - 串行端口重定向或拆分

c# - .NET 2.0 应用程序可以使用 .NET 3.5 的哪些功能?

java - TableModel 在其他复选框中第二次单击后更改 jcheckbox 单元格的值

extjs - 如何在列标题中添加复选框? ext js

c# - 运行 MSBuild 时发生内部故障 - 无法加载文件或程序集 System.Net.Primitives

C# - WPF - 防止更新绑定(bind)的焦点文本框

c# - 制作时钟 UWP (C#)

c# - Xamarin Forms Picker ItemSource 值从 XAML 本地化

c# - ResourceDictionary : {"Key cannot be null.\r\nParameter name: key"} 中的运行时错误

javascript - Angular - 如何根据数组中的值检查复选框?