Null To Boolean IValueConverter 不工作 [英] Null To Boolean IValueConverter not working

查看:38
本文介绍了Null To Boolean IValueConverter 不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 IValueConverter 将空值转换为布尔值?

How do I use an IValueConverter to convert nulls into booleans?

我正在使用 wpf 尝试显示一堆布尔值(在复选框中).创建新记录时,这些值为空,并在复选框中显示为不确定".我希望空值出现并保存为假"值.

I'm using wpf to try to display a bunch of boolean values (in checkboxes). When a new record is created, these values are null, and appear as 'indeterminate' in the checkboxes. I want the nulls to appear and save as 'false' values.

我尝试创建一个 NullToBoolean 转换器,它从数据库中获取空值并将它们显示为 false,然后在用户点击保存时将它们保存为 false.(本质上,我试图避免用户必须在复选框中单击两次(一次使其为真,然后再次使其为假).这似乎适用于导入 - 即空值显示为假 - 但除非我做了两次点击舞蹈,保存时数据库中的值不会改变.

I tried to create a NullToBoolean converter that takes null values from the database and displays them as false, and then saves them as false when the user hits save. (Essentially, I'm trying to avoid the user having to click twice in the checkboxes (once to make it true, then again to make it false). This seems to work on import - ie null values are shown as false - but unless I do the two-click dance the value doesn't change in the database when I save.

我的转换器:

[ValueConversion(typeof(bool), typeof(bool))]
public class NullBooleanConverter : IValueConverter
{

  public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  {
    if (value != null)
    {
      return value;
    }
    return false;
  }

  public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  {
    if (value != null)
    {
      return value;
    }
    return null;
  }
}

我试图让转换器使用的复选框之一:

One of the checkboxes I'm trying to have the Converter work with:

    <CheckBox Grid.Column="1" Grid.Row="0" Padding="5" Margin="5" VerticalAlignment="Center" Name="chkVarianceDescriptionProvided" IsThreeState="False">
      <CheckBox.IsChecked>
        <Binding Path="VarianceDescriptionProvided" Mode="TwoWay">
          <Binding.Converter>
            <utils:NullBooleanConverter />
          </Binding.Converter>
        </Binding>
      </CheckBox.IsChecked>
    </CheckBox>

我不知道问题是因为我的代码有问题,还是因为Converter认为没有任何变化,因此不需要ConvertBack.我已经尝试了所有 Mode 并在使用 ConvertBack 的 Convert 中切换代码,但似乎没有任何效果.

I don't know if the problem is because my code is wrong, or if it's a case of the Converter thinking that nothing has changed, therefore it doesn't need to ConvertBack. I have tried all the Modes and switched code in Convert with ConvertBack, but nothing seems to work.

有人能指出我需要做些什么来解决这个问题吗?

Can someone point out what I need to do to fix this?

推荐答案

真正的问题是您没有首先初始化数据对象.不要修复",从头做起;建设者是好的(例如).您还应该制作 ViewModels/DataModels 而不是直接使用您的模型(数据库等).

The real problem is the fact you are not initializing your data objects in the first place. Don't "fix", do it right to begin with; builders are good (for example). You also should be making ViewModels/DataModels rather than working with your Models (database, etc) directly.

public class MyObjectBuilder
{
     Checked _checked;

     public  MyObjectBuilder()
     {
          Reset()
     }

     private void Reset()
     { 
          _checked = new Checked(true); //etc
     }

     public MyObjectBuilder WithChecked(bool checked)
     {
          _checked = new Checked(checked);
     }

     public MyObject Build()
     {
         var built = new MyObject(){Checked = _checked;} 
         Reset();
         return built;
     }
}

然后总是用构建器初始化

then always initialise with the builder

myObjects.Add(new MyObjectBuilder().Build());

myObjects.Add(_injectedBuilder.Build()); // Initialises Checked to default 
myObjects.Add(_injectedBuilder.WithChecked(true).Build()); //True

虽然这不能解决您提出的问题,但它会以一种您可以进行单元测试的方式解决您的潜在问题.即您可以测试以确保添加到您的对象列表中的值始终被初始化.

While this doesn't fix your asked problem, it will fix your underlying problem in a way you can Unit Test. i.e. you can test to ensure the values added into your object list are always initialized.

这篇关于Null To Boolean IValueConverter 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆