C# 真假空组合框
我正在努力开发WPF中的组件。 这是我的问题: 我有胸部?属性,该属性要绑定到组合框的SelectedValue。 我的组合框应该有3个选项:“是”(bool==true)、“否”(bool==false)、“”(bool==null)C# 真假空组合框,c#,wpf,xaml,combobox,C#,Wpf,Xaml,Combobox,我正在努力开发WPF中的组件。 这是我的问题: 我有胸部?属性,该属性要绑定到组合框的SelectedValue。 我的组合框应该有3个选项:“是”(bool==true)、“否”(bool==false)、“”(bool==null) 我创建了一个从ComboBox继承的UserControl。在构造函数中,我用所需的值(“Yes”/“No”/”)填充ItemCollection 代码隐藏: public class YesNoNullComboBox : RadComboBox {
我创建了一个从ComboBox继承的UserControl。在构造函数中,我用所需的值(“Yes”/“No”/”)填充ItemCollection 代码隐藏:
public class YesNoNullComboBox : RadComboBox
{
public YesNoNullComboBox()
{
this.Items.Add("Yes");
this.Items.Add("No");
this.Items.Add(string.Empty);
}
}
XAML:
我设法让一些东西按我想要的方式运行,但为此,我不得不使用转换器。因此,我有一个UserControl和一个转换器要添加到我的XAML中
我曾考虑将UserControl设置为默认使用转换器,但我没有弄清楚如何。。
这可能吗?或者有更好的方法吗?ValueConverters的目的是绑定两个具有不兼容类型的属性。在这里,您希望布尔值变成字符串。对字符串的隐式强制转换为“true”或“false”,但不是所需的字符串
在我看来,ValueConverters是针对您的情况的最佳和最简单的解决方案。ValueConverters的目的是绑定两个具有不兼容类型的属性。在这里,您希望布尔值变成字符串。对字符串的隐式强制转换为“true”或“false”,但不是所需的字符串
在我看来,ValueConverters是针对您的情况的最佳和最简单的解决方案。您必须在绑定声明中指定转换器,但您可以在内部执行此操作。
我已经画了这个: 1) 用于存储字符串值以在组合框和转换器中操作的帮助器类:
public static class ThreeStateComboBoxItemsSource
{
public const string None = "(none)";
public const string Yes = "Yes";
public const string No = "No";
public static readonly string[] ItemsSource = new[]
{
None,
Yes,
No
};
}
[ValueConversion(typeof(bool?), typeof(string))]
public sealed class NullableBoolToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return ThreeStateComboBoxItemsSource.None;
return (bool)value ? ThreeStateComboBoxItemsSource.Yes : ThreeStateComboBoxItemsSource.No;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
switch ((string)value)
{
case ThreeStateComboBoxItemsSource.Yes:
return true;
case ThreeStateComboBoxItemsSource.No:
return false;
default:
return null;
}
}
}
2) 转换器:
public static class ThreeStateComboBoxItemsSource
{
public const string None = "(none)";
public const string Yes = "Yes";
public const string No = "No";
public static readonly string[] ItemsSource = new[]
{
None,
Yes,
No
};
}
[ValueConversion(typeof(bool?), typeof(string))]
public sealed class NullableBoolToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return ThreeStateComboBoxItemsSource.None;
return (bool)value ? ThreeStateComboBoxItemsSource.Yes : ThreeStateComboBoxItemsSource.No;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
switch ((string)value)
{
case ThreeStateComboBoxItemsSource.Yes:
return true;
case ThreeStateComboBoxItemsSource.No:
return false;
default:
return null;
}
}
}
3) 用户控制。我不希望用户能够覆盖CombeBox
的行为(例如,设置另一个ItemsSource
),我将CB
包装到UserControl
中,而不是继承:
XAML:
4) 使用示例:
<local:ThreeStateComboBox Value="{Binding MyProperty1}"/>
由于转换器是在用户控件内部使用的,您不需要在其他地方使用它,只需将
bool
/bool?
属性绑定到Value
您必须在绑定声明中指定转换器,但您可以在内部执行此操作。我已经画了这个: 1) 用于存储字符串值以在组合框和转换器中操作的帮助器类:
public static class ThreeStateComboBoxItemsSource
{
public const string None = "(none)";
public const string Yes = "Yes";
public const string No = "No";
public static readonly string[] ItemsSource = new[]
{
None,
Yes,
No
};
}
[ValueConversion(typeof(bool?), typeof(string))]
public sealed class NullableBoolToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return ThreeStateComboBoxItemsSource.None;
return (bool)value ? ThreeStateComboBoxItemsSource.Yes : ThreeStateComboBoxItemsSource.No;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
switch ((string)value)
{
case ThreeStateComboBoxItemsSource.Yes:
return true;
case ThreeStateComboBoxItemsSource.No:
return false;
default:
return null;
}
}
}
2) 转换器:
public static class ThreeStateComboBoxItemsSource
{
public const string None = "(none)";
public const string Yes = "Yes";
public const string No = "No";
public static readonly string[] ItemsSource = new[]
{
None,
Yes,
No
};
}
[ValueConversion(typeof(bool?), typeof(string))]
public sealed class NullableBoolToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return ThreeStateComboBoxItemsSource.None;
return (bool)value ? ThreeStateComboBoxItemsSource.Yes : ThreeStateComboBoxItemsSource.No;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
switch ((string)value)
{
case ThreeStateComboBoxItemsSource.Yes:
return true;
case ThreeStateComboBoxItemsSource.No:
return false;
default:
return null;
}
}
}
3) 用户控制。我不希望用户能够覆盖CombeBox
的行为(例如,设置另一个ItemsSource
),我将CB
包装到UserControl
中,而不是继承:
XAML:
4) 使用示例:
<local:ThreeStateComboBox Value="{Binding MyProperty1}"/>
由于转换器是在用户控件内部使用的,您不需要在其他地方使用它,只需将
bool
/bool?
属性绑定到从组合框继承的值-用户控件旨在聚合另一个控件,而不是继承它们。我没有完全理解你所说的第三个para的意思。我刚刚编辑了我的问题以使其更清晰。这是你想要的:不,不幸的是,这不是我要找的。“从组合框继承的用户控件”-用户控件旨在聚合另一个控件,而不是继承它们。我没有完全理解你所说的第三个para的意思。我刚刚编辑了我的问题以使其更清晰。这是你想要的:不,不幸的是,这不是我要找的。这是我的第一个猜测,我是这样做的。让我烦恼的是,我的自定义UserControl随后在两个不同的部分中实现。一个是用户控件,另一个是转换器。我有没有办法把这些部分收集起来?因此,我不必每次都在XAML中添加UserControl和转换器,为什么不呢?这取决于你的用户控制,没什么好羞愧的。无论如何,我看不到另一个干净的解决方案。这是我的第一个猜测,我是这样想的。让我烦恼的是,我的自定义UserControl随后在两个不同的部分中实现。一个是用户控件,另一个是转换器。我有没有办法把这些部分收集起来?因此,我不必每次都在XAML中添加UserControl和转换器,为什么不呢?这取决于你的用户控制,没什么好羞愧的。无论如何,我看不到另一个干净的解决方案。这正是我想要的。非常感谢你,丹尼斯。这正是我想要的。非常感谢你,丹尼斯。