设置 WPF ComboBox 的 SelectedItem
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3391195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set SelectedItem of WPF ComboBox
提问by knockando
<ComboBox Grid.Row="1" Grid.Column="0" Width="Auto" Name="cmbBudgetYear">
<ComboBoxItem Content="2009" />
<ComboBoxItem Content="2010" />
<ComboBoxItem Content="2011" />
<ComboBoxItem Content="2012" />
</ComboBox>
How do I set the selected item to the current year in the code behind?
如何在后面的代码中将所选项目设置为当前年份?
Something like...
就像是...
cmbBudgetYear.SelectedItem = cmbBudgetYear.Items(
get the item with the Now.Year.ToString)
回答by HCL
There exist many ways to do this but for your example, I would change the ComboBox-Tag as follows:
有很多方法可以做到这一点,但对于您的示例,我将按如下方式更改 ComboBox-Tag:
<ComboBox Grid.Row="1" Grid.Column="0"
Name="cmbBudgetYear" SelectedValuePath="Content">
I added the attribute-defition SelectedValuePath="Content"
. After that you can set the value with a corresponding string, e.g.:
我添加了属性定义SelectedValuePath="Content"
。之后,您可以使用相应的字符串设置该值,例如:
cmbBudgetYear.SelectedValue = "2009";
Take care that the value must be a string. For your example, use
注意该值必须是字符串。对于您的示例,请使用
cmbBudgetYear.SelectedValue = DateTime.Now.Year.ToString();
An additional idea
一个额外的想法
If you use the code-behind anyway, would it be a possibility to fill the combobox with integers. Someting like:
如果您无论如何都使用代码隐藏,是否有可能用整数填充组合框。有点像:
for(int y=DateTime.Now.Year;y>DateTime.Now.Year-10;y--){
cmbBudgetYear.Items.Add(y);
}
..then you can select the values extremly simple like
..然后你可以选择非常简单的值
cmbBudgetYear.SelectedValue = 2009;
... and you would have also other advantages.
......你还有其他优势。
回答by Simone Brognoli
In my case I added the values manually with:
在我的情况下,我手动添加了值:
myComboBox.Items.Add("MyItem");
and then I select the wanted one with:
然后我选择了想要的:
myComboBox.SelectedItem = "WantedItem";
instead of:
代替:
myComboBox.SelectedValue = "WantedItem";
回答by vapcguy
In this case, you should be able to simply use .Text()
to set it:
在这种情况下,您应该能够简单地使用.Text()
来设置它:
cmbBudgetYear.Text = "2010";
For getting the value after a change, though, and maybe it's because I didn't set SelectedValuePath="Content"
everywhere, or maybe because I didn't use SelectedValue
to set it (and why I'm mentioning it), it becomes slightly more complicated to determine the actual value, as you have to do this after adding the event handler for SelectionChanged
in the XAML:
但是,为了在更改后获取值,可能是因为我没有SelectedValuePath="Content"
在任何地方设置,或者可能是因为我没有SelectedValue
设置它(以及我为什么提到它),确定实际值,因为您必须SelectionChanged
在 XAML 中添加事件处理程序后执行此操作:
private void cmbBudgetYear_SelectionChanged(object sender, EventArgs e)
{
ComboBox cbx = (ComboBox)sender;
string yourValue = String.Empty;
if (cbx.SelectedValue == null)
yourValue = cbx.SelectionBoxItem.ToString();
else
yourValue = cboParser(cbx.SelectedValue.ToString());
}
Where a parser is needed because .SelectedValue.ToString()
will give you something like System.Windows.Controls.Control: 2010
, so you have to parse it out to get the value:
需要解析器的地方,因为.SelectedValue.ToString()
会给你类似的东西System.Windows.Controls.Control: 2010
,所以你必须解析它才能得到值:
private static string cboParser(string controlString)
{
if (controlString.Contains(':'))
{
controlString = controlString.Split(':')[1].TrimStart(' ');
}
return controlString;
}
At least, this is what I ran into.... I know this question was about setting the box, but can't address only setting without talking about how to get it, later, too, as how you set it will determine how you get it if it is changed.
至少,这就是我遇到的......我知道这个问题是关于设置框的,但不能只解决设置而不谈论如何获得它,稍后也是如此,因为你如何设置它将决定如何如果它被改变,你会得到它。
回答by Developer
It works fine for me.
这对我来说可以。
ObservableCollection<OrganizationView> Organizations { get; set; }
Organizations = GetOrganizations();
await Dispatcher.BeginInvoke((Action)(() =>
{
var allOrganizationItem = new OrganizationView() { ID = 0, IsEnabled = true, Name = "(All)" }; // It is a class
Organizations.Add(allOrganizationItem);
cbOrganizations.DisplayMemberPath = "Name";
cbOrganizations.SelectedValuePath = "ID";
cbOrganizations.ItemsSource = null;
cbOrganizations.ItemsSource = Organizations; // Set data source which has all items
cbOrganizations.SelectedItem = allOrganizationItem; // It will make it as a selected item
}));