Pra*_*apa 21 .net c# wpf visual-studio-2010
我习惯这样做
State.Items.Add(new ListItem { Text = "SomeState", Value = NumericIDofState });
Run Code Online (Sandbox Code Playgroud)
State是ASP.NET中的列表框.
我如何使用WPF ComboBox实现相同的目标?我确实在ComboBoxItem对象中看到了一个名为"Content"的属性,但是如何为每个项目分配一个值而不是显示给用户的值?请帮忙.
Ale*_*mov 38
SelectedValuePath
property,指定用于确定SelectedValue
属性值的属性的路径.它类似于ASP.NET ListItem
的Value
属性.DisplayMemberPath
定义描述如何显示数据对象的默认模板的属性.它类似于ASP.NET ListItem
的Text
属性.假设您希望Combobox
显示以下KeyValuePair
对象的集合:
private static readonly KeyValuePair<int, string>[] tripLengthList = {
new KeyValuePair<int, string>(0, "0"),
new KeyValuePair<int, string>(30, "30"),
new KeyValuePair<int, string>(50, "50"),
new KeyValuePair<int, string>(100, "100"),
};
Run Code Online (Sandbox Code Playgroud)
您在视图模型中定义一个返回该集合的属性:
public KeyValuePair<int, string>[] TripLengthList
{
get
{
return tripLengthList;
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您的XAML Combobox
将是:
<ComboBox
SelectedValue="{Binding FilterService.TripLengthFrom, Mode=TwoWay}"
ItemsSource="{Binding TripLengthList, Mode=OneTime}"
SelectedValuePath="Key"
DisplayMemberPath="Value" />
Run Code Online (Sandbox Code Playgroud)
在哪里设置SelectedValuePath
和DisplayMemberPath
属性为对象的所需属性名称(Key
并Value
相应地)由显示Combobox
.
或者,如果您真的想要Combobox
在代码后面添加项而不是使用绑定,那么您也可以这样做.例如:
<!--XAML-->
<ComboBox x:Name="ComboBoxFrom"
SelectedValue="{Binding FilterService.TripLengthFrom, Mode=TwoWay}" />
// Code behind
public partial class FilterView : UserControl
{
public FilterView()
{
this.InitializeComponent();
this.ComboBoxFrom.SelectedValuePath = "Key";
this.ComboBoxFrom.DisplayMemberPath = "Value";
this.ComboBoxFrom.Items.Add(new KeyValuePair<int, string>(0, "0"));
this.ComboBoxFrom.Items.Add(new KeyValuePair<int, string>(30, "30"));
this.ComboBoxFrom.Items.Add(new KeyValuePair<int, string>(50, "50"));
this.ComboBoxFrom.Items.Add(new KeyValuePair<int, string>(100, "100"));
}
Run Code Online (Sandbox Code Playgroud)
TGa*_*sdf 29
如果您只想在viewmodel中公开一个简单的属性并处理视图中选项的文本,您可以执行如下简单的解决方案:
<ComboBox SelectedValuePath="Tag" SelectedValue="{Binding YourIntProperty, Mode=TwoWay}">
<ComboBoxItem Content="First choice" Tag="0"/>
<ComboBoxItem Content="Second choice" Tag="1"/>
<ComboBoxItem Content="Third choice" Tag="2"/>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)
bool属性的示例:
<ComboBox SelectedValuePath="Tag" SelectedValue="{Binding IsActive, Mode=TwoWay}">
<ComboBoxItem Content="No" Tag="False"/>
<ComboBoxItem Content="Yes" Tag="True"/>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)
类型 - 详细替代(原始示例)
下面是明确声明类型的更详细的替代方案.根据您的喜好风格(或某些需要它的类型),也许它更适合您.
<ComboBox SelectedValuePath="Tag" SelectedValue="{Binding YourIntProperty, Mode=TwoWay}">
<ComboBoxItem Content="First choice">
<ComboBoxItem.Tag>
<sys:Int32>0</sys:Int32>
</ComboBoxItem.Tag>
</ComboBoxItem>
<ComboBoxItem Content="Second choice">
<ComboBoxItem.Tag>
<sys:Int32>1</sys:Int32>
</ComboBoxItem.Tag>
</ComboBoxItem>
<ComboBoxItem Content="Third choice">
<ComboBoxItem.Tag>
<sys:Int32>2</sys:Int32>
</ComboBoxItem.Tag>
</ComboBoxItem>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)
bool属性的示例:
<ComboBox SelectedValuePath="Tag" SelectedValue="{Binding IsActive, Mode=TwoWay}">
<ComboBoxItem Content="No">
<ComboBoxItem.Tag>
<sys:Boolean>False</sys:Boolean>
</ComboBoxItem.Tag>
</ComboBoxItem>
<ComboBoxItem Content="Yes">
<ComboBoxItem.Tag>
<sys:Boolean>True</sys:Boolean>
</ComboBoxItem.Tag>
</ComboBoxItem>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)
sys命名空间声明为:
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Run Code Online (Sandbox Code Playgroud)