WPFコンボボックスをMyEnum型のnullableプロパティにバインドしていますか?私はプログラムでこのようなコンボボックスの項目移入AM(ここMyEnumが列挙型である)Nullable値をWPFコンボボックスにバインドする際の問題
:コードビハインドも
// The enum type being bound to
enum MyEnum { Yes, No }
// Helper class for representing combobox listitems
// (a combination of display string and value)
class ComboItem {
public string Display {get;set}
public MyEnum? Value {get;set}
}
private void LoadComboBoxItems()
{
// Make a list of items to load into the combo
var items = new List<ComboItem> {
new ComboItem {Value = null, Display = "Maybe"},
new ComboItem {Value = MyEnum.Yes, Display = "Yes"},
new ComboItem {Value = MyEnum.No, Display = "No"},};
// Bind the combo's items to this list.
theCombo.ItemsSource = items;
theCombo.DisplayMemberPath = "Display";
theCombo.SelectedValuePath = "Value";
}
を、私はプロパティと、クラスのインスタンスへのDataContextを設定していMyEnum?タイプのTheNullableProperty(この例ではとにかく)と呼ばれます。
ComboのSelectedValueのバインドは、私のXAMLファイルで行われます。
<ComboBox
Name="theCombo"
SelectedValue="{Binding Path=TheNullableProperty,
UpdateSourceTrigger=PropertyChanged}"/>
問題:
バウンドプロパティの値は、最初は非nullで、コンボボックスが正しく値を表示します。
ただし、バインドされたプロパティの値が最初はnullの場合、コンボボックスは空白です。
コンボボックスが最初に表示されたときに、データバインディングのすべての側面がnull値の表記とは別の働きをしているようです。
たとえば、ドロップダウンから[多分]を選択すると、バインドされたプロパティが正しくnullに設定されます。最初のローディングが失敗しただけです。たぶん私はちょうど手動で最初にSelectedValueのを設定し...私は
- をやってしまった何
がNULL可能に列挙型から変換コンバータを介して基本的なNULL可能列挙値に隠されたテキストブロックデータバインドを追加する必要がありますstring(enum.ToString、または "null")。
- コンボボックスには、文字列Label(コンボで表示)と文字列(文字列としての列値)(null値の場合は「null」)をそれぞれ持つ「ComboItems」をロードします。
コンボボックスをテキストブロックにデータバインドします。
/// <summary> /// Convert from EnumeratedType? to string (null->"null", enum values->ToString) /// </summary> public class EnumConverter<T> : IValueConverter where T:struct { public static string To(T? c) { if (c == null) return "null"; return c.ToString(); } public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return To((T?)value); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { var s = (string) value; if (s == "null") return null; return (T?)Enum.Parse(typeof(T), s); } } public class MyEnumConverter : EnumConverter<MyEnum> { } public class ComboItem { public string Value { get; set; } public string Label { get; set; } public ComboItem(MyEnum? e, string label) { Value = MyEnumConverter.To(e); Label = label; } } static IEnumerable<ComboItem> GetItems() { yield return new ComboItem(null, "maybe"); yield return new ComboItem(MyEnum.Yes, "yup"); yield return new ComboItem(MyEnum.No, "nope"); } private void SetupComboBox() { thecombo.ItemsSource = GetItems().ToList(); }
nullはItemsControlsの場合の特別な値だと思います。コンバータを提供している間は、例えばTextBlockにnullをバインドできます。 Aコンバーターは、ItemsControlsが選択された項目としてAFAICSを受け入れることを助けるものではありません。 – mackenir