我想将组合框的选定值绑定到有界对象属性,并将每个组合框的选定索引设置为0。 问题是只有第一个组合显示所选项目。
public enum SubEnum1
{
Apple=1,
Banana=2,
Pear=3
}
public enum FullEnum
{
Apple=1,
Banana=2,
Pear=3,
Cucumber=4,
Tomato=5,
Onion=6
}
Im XAML窗口我有一些数据控件(列表),其中是一个带有组合框的datatemplate。
组合框与SubEnum1绑定。
数据控件与对象集合有关:
List<MyObject> collection = new List<MyObject>()
//collection.Add...
mylist.ItemsSource = collection;
public class MyObject
{
public FullEnum TheSelectedEnum {get;set;}
....
//other properties
}
public class EnumConverter2 : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null)
return (FullEnum)value;
else return "";
}
}
<ObjectDataProvider x:Key="Enum1"
MethodName="GetValues"
ObjectType="{x:Type sys:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="local:SubEnum1" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<ListBox Height="261" HorizontalAlignment="Left" Name="mylist" VerticalAlignment="Top" Width="278">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<ComboBox Height="23" Width="90"
ItemsSource="{Binding Source={StaticResource Enum1}}"
SelectedValue="{Binding Path=TheSelectedEnum, Converter={StaticResource enumConverter}}"
SelectedIndex="0"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
(如果我扩展其他组合框,我会看到值)
更新发布:
也许我可以以其他方式将所选值传递给绑定对象?
答案 0 :(得分:1)
您可以这样做并解决您的问题:
public enum SubEnum1
{
None=0,
Apple=1,
Banana=2,
Pear=3
}
然后使用FallbackValue:
<ComboBox Height="23" Width="90"
ItemsSource="{Binding Source={StaticResource Enum1}}"
SelectedValue="{Binding Path=TheSelectedEnum, FallbackValue=0}" />