c# - WPF 是否转换器

标签 c# wpf

我的表中有一个位字段需要转换为与 1 或 0 相关的是或否。 到目前为止,我一直在使用这样的转换器,但效果不佳。

因为我绑定(bind)到一个组合框,所以我需要在代码中填充它,但是没有一个字段可以将 DisplayMemberPath 和 SelectedValuePath 设置为。

此外,我的 debugger.break() 也不工作。

感谢您的帮助

public class BooleanToYesNoConverter : IValueConverter
{
  public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
     Debugger.Break();
     if (value == null)
        return "No";

     bool inValue = (bool)value;
     string outValue = inValue ? "Yes" : "No";
     return outValue;
  }

  public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
     Debugger.Break();
     if (value == null)
        return 0;

     int? outValue;
     string inValue = (string)value;

     inValue = inValue.Trim();

     if (inValue == "Yes")
     {
        outValue = 1;
     }
     else
        if (inValue == "No")
        {
           outValue = 0;
        }
        else
        {
           return DependencyProperty.UnsetValue;
        }
     return outValue;
  }

这是我在我的 ViewModel 中绑定(bind)到的属性

private BindableCollection<string> _licensedBitDisplay;
public BindableCollection<string> LicensedBitDisplay
{
   get { return _licensedBitDisplay; }
   set { SetValueAndNotify(() => LicensedBitDisplay, ref _licensedBitDisplay, value); }
}

和填充下拉列表的代码

LicensedBitDisplay = new BindableCollection<string>();
LicensedBitDisplay.AddRange(new List<string>() { "No", "Yes" });

最后是xaml

<ComboBox Margin="24,3,0,3" Width="162" HorizontalAlignment="left" telerik:StyleManager.Theme="Office_Blue" 
    ItemsSource="{Binding Path=LicensedBitDisplay}" 
    SelectedValue="{Binding Path=CurrentEntity.Licensed, Mode=TwoWay, 
    Converter={StaticResource BooleanToYesNoConverter1},
    diag:PresentationTraceSources.TraceLevel=High}" />

最佳答案

您的转换是反向的,因为绑定(bind)源 (LicensedBitDisplay) 包含字符串。

Convert 从源转换为目标。源是 ViewModel,目标是绑定(bind)到它的 UI 控件)。 ConvertBack 从目标转换为源。这通常仅在您有接受用户输入的控件时有用(例如,用户在文本框中键入"is"并且转换器将 1 提供给 ViewModel 属性)。

要使其正常工作,LicensedBitDisplay 应该是 int? 的集合。此外,您当前的 Convert 实现将失败,因为 int? 无法转换为 bool。相反,您可以使用 System.Convert.ToBoolean (这也会自动将 null 转换为 false)。该转换器应仅用于显示,在 ComboBox 的 ItemTemplate 中:

<ComboBox ItemsSource="{Binding Path=LicensedBitDisplay}" 
          SelectedValue="{Binding Path=CurrentEntity.Licensed}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Converter={StaticResource BooleanToYesNoConverter1}}" />
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

就我个人而言,我根本不喜欢使用转换器,尤其是在选择内容时。另一种表达方式是通过触发器:

<ComboBox ItemsSource="{Binding Path=LicensedBitDisplay}" 
          SelectedValue="{Binding Path=CurrentEntity.Licensed}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Name="TextBlock" Text="No" />
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding}" Value="1">
                    <Setter TargetName="TextBlock" Property="Text" Value="Yes" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

关于c# - WPF 是否转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18750134/

相关文章:

c# - 将 KeyDown 事件 (Keys) 连接到一个 C# (wpf) 字符串

WPF多按钮背景颜色变化

wpf - 调用线程无法访问该对象,因为其他线程拥有它

c# - xamarin.forms-页面底部的“滑动”信息框

c# - 通过 XmlReader 中的 XML 元素进行解析

c# - Asp.net mvc 应用程序在用户登录前抛出 js 语法错误

c# - 如何计算/确定 .NET 中的文件夹大小?

c# - 是否有理由在 C# 中始终声明类型后缀为 'f' 的 float ?

Wpf - 资源查找

c# - submitchanges() 后数据库没有更新