我想问一下,是否可以在ListBoxItem中提供将显示为的字符串和存储在DB中的值。这确实是可能的:
ItemSource={Binding MyEnumColleciton}或
ItemSource={DynamicResource MyCollection}等等。
但如果你想象我有大约100 ListBoxes ..。我不想有那么多不同的枚举和其他ItemSource集合,我想直接将它写入ListBoxItem中。
我要说的就是:
<ListBox SelectedItem="{Binding Path=MyPath1}" Style="{StaticResource RadioButtonList}">
<ListBoxItem Content="Text1" />
<ListBoxItem Content="Text2" />
</ListBox>
<ListBox SelectedItem="{Binding Path=MyPath2}" Style="{StaticResource RadioButtonList}">
<ListBoxItem Content="Text3" />
<ListBoxItem Content="Text4" />
</ListBox>
<ListBox SelectedItem="{Binding Path=MyPath3}" Style="{StaticResource RadioButtonList}">
<ListBoxItem Content="Text5" />
<ListBoxItem Content="Text6" />
</ListBox>
... 100x发布于 2010-08-05 14:05:08
我想出了这个:
public class ItemSourceProvider
{
public IEnumerable<ValueText<int>> GetValues(object o)
{
if (o == null) return null;
switch (o.ToString().ToUpper())
{
case "PARAM":
{
return new List<ValueText<int>>()
{
new ValueText<int>{Value = 1, Text = "YES"},
new ValueText<int>{Value = 2, Text = "PARTIALLY"},
new ValueText<int>{Value = 3, Text = "NO"}
};
}
default: return null;
}
}
}
public class ValueText<T>
{
public string Text { get; set; }
public T Value { get; set; }
}将DP添加到控件的资源中:
<ObjectDataProvider x:Key="testODP" MethodName="GetValues" ObjectType="{x:Type local:ItemSourceProvider}">
<ObjectDataProvider.MethodParameters>PARAM</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>然后:
<ListBox SelectedValue="{Binding Path=A}" SelectedValuePath="Value" Style="{StaticResource RadioButtonList}" DisplayMemberPath="Text" ItemsSource="{Binding Source={StaticResource testODP}}" />https://stackoverflow.com/questions/3414456
复制相似问题