2015-11-07 103 views
0

(我讀過類似的職位,但他們都有一個轉折他們做出不同的解決方案)設置所選項目

我移植WinForms應用程序所使用的:

myComboBox.SetSelected(myComboBox.FindString("Some Text"), true); 

以編程方式選擇一個項目。當移植到WPF,我試過,但它沒有任何效果(該項目不會被選中):

myComboBox.SelectedItem = myComboBox.FindName("Some Text"); 

什麼是選擇在一個ComboBox現有項目的正確方法,在WPF?

+0

什麼是你的itemsource? – Sajeetharan

+0

你可以在這裏找到答案https://www.google.com.ph/url?sa=t&source=web&rct=j&url=http://stackoverflow.com/questions/21476979/why-does-the-combobox-findname -method-always-return-null&ved = 0CBwQFjAAahUKEwiEoZyk1v3IAhUD5KYKHew9DuQ&usg = AFQjCNGMrZHFUpcCyZhk-L8amDlp_mxSIg&sig2 = KwxsuSAuv6UKhLJYOOlhVw – tgpdyk

+1

答案取決於您的代碼,[您沒有提供](http://stackoverflow.com/help/mcve)。即使您展示的Winforms示例坦率地說也不是一個好方法。在WPF中,你應該有一個綁定到'ComboBox.ItemsSource'的集合,並且你可以將SelectedItem設置爲該集合元素的引用。你如何查看它取決於代碼的確切實現。 –

回答

1

您必須使用SelectedValue。在WPF組合框中,有多種方法可以實現相同的功能。因此,以編程方式選擇項目的一種語法將不起作用。將項目添加到ComboBox有很多種方法。

  1. 您可以聲明性地或在代碼中設置ItemsSource。
  2. 您可以添加ComboBoxItems等。請參閱屬性窗口中的項目屬性以查看各種可用的項目類型。

如果您使用的ItemsSource字符串值,那麼你需要的語法,如:cmb1.SelectedValue = "Name1"

如果直接添加項目,如<ComboBox ...> <ComboBoxItem Content="Name1"/> </ComboBox/>,那麼你需要

foreach (ComboBoxItem item in cmb2.Items) 
     if (item.Content.ToString() == "Name1") 
     { 
     cmb2.SelectedValue = item; 
     break; 
     } 

我已經發布了一個完整的演示如何在各種場景中以編程方式選擇項目。示例代碼(可以按原樣使用):

注意上一個,您必須使用SelectedValuePath。

Window1.xaml

<Window x:Class="WpfApplicationBlend.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:sys="clr-namespace:System;assembly=mscorlib" 
    Title="Window1" Height="411" Width="749"> 

<Grid> 
    <Grid Margin="30,27,491,276"> 
     <ComboBox x:Name="cmb1" HorizontalAlignment="Left" Margin="0,28,0,0" VerticalAlignment="Top" Width="210" Height="25" FocusVisualStyle="{DynamicResource StyleFocusDefault}"> 
      <ComboBox.ItemsSource> 
       <CompositeCollection> 
        <sys:String>Name1</sys:String> 
        <sys:String>Name2</sys:String> 
        <sys:String>Name3</sys:String> 
        <sys:String>Name4</sys:String> 
       </CompositeCollection> 
      </ComboBox.ItemsSource> 
     </ComboBox> 
     <TextBox x:Name="tbInput1" HorizontalAlignment="Left" Height="23" Margin="10,0,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/> 
     <Button Content="Button" HorizontalAlignment="Left" Margin="135,1,0,0" VerticalAlignment="Top" Width="75" Click="Button1_Click"/> 
    </Grid> 

    <Grid Margin="405,27,111,276"> 
     <ComboBox x:Name="cmb2" HorizontalAlignment="Left" Margin="0,28,0,0" VerticalAlignment="Top" Width="210" Height="25" FocusVisualStyle="{DynamicResource StyleFocusDefault}"> 
      <ComboBoxItem Content="Name1"/> 
      <ComboBoxItem Content="Name2"/> 
      <ComboBoxItem Content="Name3"/> 
     </ComboBox> 
     <TextBox x:Name="tbInput2" HorizontalAlignment="Left" Height="23" Margin="10,0,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/> 
     <Button Content="Button" HorizontalAlignment="Left" Margin="135,1,0,0" VerticalAlignment="Top" Width="75" Click="Button2_Click"/> 
    </Grid> 

    <Grid Margin="30,207,491,96"> 
     <ComboBox x:Name="cmb3" HorizontalAlignment="Left" Margin="0,28,0,0" VerticalAlignment="Top" Width="210" Height="25" FocusVisualStyle="{DynamicResource StyleFocusDefault}"> 
      <ComboBox.ItemsSource> 
       <CompositeCollection> 
        <sys:String>Name1</sys:String> 
        <sys:Boolean>True</sys:Boolean> 
        <sys:Int32>123</sys:Int32> 
       </CompositeCollection> 
      </ComboBox.ItemsSource> 
     </ComboBox> 
     <TextBox x:Name="tbInput3" HorizontalAlignment="Left" Height="23" Margin="10,0,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/> 
     <Button Content="Button" HorizontalAlignment="Left" Margin="135,1,0,0" VerticalAlignment="Top" Width="75" Click="Button3_Click"/> 
    </Grid> 

    <Grid Margin="405,207,116,96"> 
     <ComboBox x:Name="cmb4" HorizontalAlignment="Left" Margin="0,28,0,0" VerticalAlignment="Top" Width="210" Height="25" SelectedValuePath="Name" DisplayMemberPath="Name"> 
     </ComboBox> 
     <TextBox x:Name="tbInput4" HorizontalAlignment="Left" Height="23" Margin="10,0,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/> 
     <Button Content="Button" HorizontalAlignment="Left" Margin="135,1,0,0" VerticalAlignment="Top" Width="75" Click="Button4_Click"/> 
    </Grid> 
</Grid> 
    </Window> 

Window1.xaml.cs

using System; 
using System.Collections.Generic; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Collections; 

namespace WpfApplicationBlend 
{ 
    /// <summary> 
    /// Interaction logic for Window1.xaml 
    /// </summary> 
    public partial class Window1 : Window 
    { 
     public Window1() 
     { 
      InitializeComponent(); 

      List<Employee> employees = new List<Employee>() 
      { 
       new Employee(){Name="Name1", Age=100}, 
       new Employee(){Name="Name2", Age=101}, 
      }; 

      cmb4.ItemsSource = employees; 
     } 

     private void Button1_Click(object sender, RoutedEventArgs e) 
     { 
      cmb1.SelectedValue = tbInput1.Text; 
     } 

     private void Button2_Click(object sender, RoutedEventArgs e) 
     { 
      foreach (ComboBoxItem item in cmb2.Items) 
       if (item.Content.ToString() == tbInput2.Text) 
       { 
        cmb2.SelectedValue = item; 
        break; 
       } 
     } 

     private void Button3_Click(object sender, RoutedEventArgs e) 
     { 
      foreach (object item in cmb3.Items) 
       if (item.ToString() == tbInput3.Text) 
       { 
        cmb3.SelectedValue = item; 
        break; 
       } 
     } 

     private void Button4_Click(object sender, RoutedEventArgs e) 
     { 
      cmb4.SelectedValue = tbInput4.Text; 
     } 
    } 

    public class Employee 
    { 
     public string Name { get; set; } 
     public int Age { get; set; } 
    } 
} 
-1
comboboxName.SelectedIndex = yourIndex; 

例如

combobox1.SelectedIndex = 2; 
相關問題