Skip to main content
 首页 » 编程设计

c#之WPF 是否转换器

2024年02月13日20sxdcgaq8080

我的表中有一个位字段需要转换为与 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>