Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF-YesNo转换器_C#_Wpf - Fatal编程技术网

C# WPF-YesNo转换器

C# WPF-YesNo转换器,c#,wpf,C#,Wpf,我的表中有一个位字段,我需要将其转换为与1或0相关的是或否。 到目前为止,我使用的是一个这样的转换器,它还不能正常工作 由于我绑定到一个组合框,我需要在代码中填充它,但是没有一个字段来设置DisplayMemberPath和SelectedValuePath 另外,我的debugger.break也不工作 谢谢你的帮助 public class BooleanToYesNoConverter : IValueConverter { public object Convert(object v

我的表中有一个位字段,我需要将其转换为与1或0相关的是或否。 到目前为止,我使用的是一个这样的转换器,它还不能正常工作

由于我绑定到一个组合框,我需要在代码中填充它,但是没有一个字段来设置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中绑定到的属性

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}" />

您的转换是向后的,因为绑定源LicensedBitDisplay包含字符串

转换从源到目标。源是ViewModel,目标是绑定到它的UI控件。 ConvertBack从目标转换为源。这通常仅在控件接受用户输入时有用,例如,用户在文本框中键入Yes,并且转换器将ViewModel属性设为1

为了实现这一点,LicensedBitDisplay应该是int?的集合?。此外,您当前的Convert实现将失败,因为int?不能投给布尔。相反,您可以使用它,它还将自动将null转换为false。在组合框的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>

您的转换是向后的,因为绑定源LicensedBitDisplay包含字符串

转换从源到目标。源是ViewModel,目标是绑定到它的UI控件。 ConvertBack从目标转换为源。这通常仅在控件接受用户输入时有用,例如,用户在文本框中键入Yes,并且转换器将ViewModel属性设为1

为了实现这一点,LicensedBitDisplay应该是int?的集合?。此外,您当前的Convert实现将失败,因为int?不能投给布尔。相反,您可以使用它,它还将自动将null转换为false。在组合框的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>


所以问题是什么?为什么返回的是对象而不是字符串?另外,看起来您在Convert和ConvertBack中没有使用其他参数,为什么要传入它们呢?避免麻烦,在ComboBox中使用ItemTemplate。@Jonesy:因为IValueConverter就是这样的defined@Jonesy你的问题是无效的,与OP试图做的事情无关。如果他没有使用Convert和ConvertBack中的其他参数,这并不重要。因此。。问题是什么?为什么返回的是对象而不是字符串?另外,看起来您在Convert和ConvertBack中没有使用其他参数,为什么要传入它们呢?避免麻烦,在ComboBox中使用ItemTemplate。@Jonesy:因为IValueConverter就是这样的defined@Jonesy你的问题是无效的,与OP试图做的事情无关。如果他没有使用Convert和ConvertBack中的其他参数,这并不重要。谢谢您的回复。如果我让LicensedBitDisplay显示一个可为null的int类型,那么我必须用整数填充它。但是我需要下拉列表来显示是/否?。所以我需要在Itemsource上调用Converter?当我这样做时,下拉列表将被删除,并且不会填充。我确实对转换器进行了强制转换更改。我在加载时调试了实体,并且许可字段为true。@RobDog888实际上,转换器需要位于ItemTemplate中。我更新了答案。我尝试了更新的代码,但两个版本都无法运行。我尝试了转换器示例和内联示例。我在内联中遇到此错误,实体未获得脏标志-无法将“0”从类型“Int32”转换为类型“System.Boolean”,对于“en-US”区域性,使用默认转换;考虑使用绑定属性的转换器属性…@ Rabdo88。如果许可的属性是布尔值,你可以使用true而不是1。谢谢你的回复。如果我让LicensedBitDisplay显示一个可为null的int类型,那么我必须用整数填充它。但是我需要下拉列表来显示是/否?。所以我需要在Itemsource上调用Converter?当我这样做时,下拉列表将被删除,并且不会填充。我确实对转换器进行了强制转换更改。我在加载时调试了实体,并且许可字段为true。@RobDog888实际上,转换器需要位于ItemTemplate中。我更新了答案。我尝试了更新的代码,但两个版本都无法运行。我尝试了转换器示例和内联示例。我在内联中遇到此错误,而实体未获得脏标志-无法将“0”从类型“Int32”转换为类型“System.Boolean”,表示“en US”c
与默认的转换文化;考虑使用绑定的转化器属性…@ Rabdo88。如果许可的属性是布尔型的,您可以只使用TRUE而不是1。