C# ListBoxItem是否有值?
我想请您询问,是否有可能在C# ListBoxItem是否有值?,c#,wpf,xaml,listbox,listboxitem,C#,Wpf,Xaml,Listbox,Listboxitem,我想请您询问,是否有可能在ListBoxItem中提供将出现的字符串以及存储在DB中的值。 这确实是可能的: ItemSource={Binding MyEnumColleciton} 或 等等 但是如果你想象我有大约100个列表框。。我不想有这么多不同的枚举和其他ItemSource集合,我想直接将其写入ListBoxItem 这就是我要说的: <ListBox SelectedItem="{Binding Path=MyPath1}" Style="{StaticResource R
ListBoxItem
中提供将出现的字符串以及存储在DB中的值。
这确实是可能的:
ItemSource={Binding MyEnumColleciton}
或
等等
但是如果你想象我有大约100个列表框。。我不想有这么多不同的枚举和其他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
... 100倍
我想到了这个:
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; }
}
公共类ItemSourceProvider
{
公共IEnumerable GetValues(对象o)
{
如果(o==null)返回null;
开关(o.ToString().ToUpper())
{
案例“PARAM”:
{
返回新列表()
{
新值Text{Value=1,Text=“YES”},
新值Text{Value=2,Text=“部分”},
新值Text{Value=3,Text=“NO”}
};
}
默认值:返回null;
}
}
}
公共类价值文本
{
公共字符串文本{get;set;}
公共T值{get;set;}
}
将DP添加到控件的资源中:
<ObjectDataProvider x:Key="testODP" MethodName="GetValues" ObjectType="{x:Type local:ItemSourceProvider}">
<ObjectDataProvider.MethodParameters>PARAM</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
PARAM
然后:
<ListBox SelectedValue="{Binding Path=A}" SelectedValuePath="Value" Style="{StaticResource RadioButtonList}" DisplayMemberPath="Text" ItemsSource="{Binding Source={StaticResource testODP}}" />
这不是我想要的。。但是你的意见?
<ListBox SelectedValue="{Binding Path=A}" SelectedValuePath="Value" Style="{StaticResource RadioButtonList}" DisplayMemberPath="Text" ItemsSource="{Binding Source={StaticResource testODP}}" />