[英]ListBoxItem with value?
我想問問是否有可能在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
好吧,我想到了這個:
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}}" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.