2014-07-10 176 views
0

我已經創建了WPF應用程序。我有一個組合框的顏色。我希望選擇的顏色是我的ComboBoxItem,但是當我從ComboBox中選擇一個項目時,它顯示'System.Windows.Controls.ComboBoxItem'而不是項目(這是我的顏色名稱)。WPF組合框SelectedItem未顯示其值

下面是一個組合框一個XAML代碼:

<ComboBox Height="23" HorizontalAlignment="Right" Margin="0,330,557,0" Name="comboBox_PC_Opt" VerticalAlignment="Top" Width="130" IsEditable="True" SelectionChanged="comboBox_PC_Opt_SelectionChanged"> 
      <ComboBoxItem VerticalContentAlignment="Center"> 
       <StackPanel Orientation="Horizontal"> 
        <Rectangle Fill="Blue" Width="15" Height="15" Margin="0,2,5,2" /> 
        <TextBlock Text="Blue" /> 
       </StackPanel> 
      </ComboBoxItem> 
      <ComboBoxItem VerticalContentAlignment="Center"> 
       <StackPanel Orientation="Horizontal"> 
        <Rectangle Fill="Black" Width="15" Height="15" Margin="0,2,5,2" /> 
        <TextBlock Text="Black" /> 
       </StackPanel> 
      </ComboBoxItem></ComboBox> 

所以,我怎麼能解決這個..

回答

0

一個簡單的解決方案是使用TextSearch.TextPath附屬性。你可以試試這個代碼:

<ComboBox Height="23" HorizontalAlignment="Right" Margin="0,330,557,0" 
      Name="comboBox_PC_Opt" VerticalAlignment="Top" Width="130" 
      IsEditable="True" 
      SelectionChanged="comboBox_PC_Opt_SelectionChanged" 
      TextSearch.TextPath="Content.Children[1].Text" 
> 

注意,ComboBoxItem應該包含兒童(如在你的代碼)一致(這樣Children[1]指向正確的TextBlock)。

UPDATE:如果您只是想在顏色名稱旁邊顯示彩色矩形而不需要使組合框可編輯,您可以刪除IsEditable="True"

要獲得所選擇的顏色,你必須使用SelectedValuePath指向內RectangleFill.Color,當然這樣的ComboBoxItem應該總是包含一個StackPanel這個面板應該包含顏色Rectangle作爲第一個項目:

<ComboBox Height="23" HorizontalAlignment="Right" Margin="0,330,557,0" 
      Name="comboBox_PC_Opt" VerticalAlignment="Top" Width="130" 
      IsEditable="True" 
      SelectionChanged="comboBox_PC_Opt_SelectionChanged" 
      TextSearch.TextPath="Content.Children[1].Text" 
      SelectedValuePath="Content.Children[0].Fill.Color" 
> 

然後在後面的代碼,你可以得到的SelectedValue這樣的:

private void comboBox_PC_Opt_SelectionChanged(object sender, 
               SelectionChangedEventArgs e){ 
    var selectedValue = ((ComboBox)e.Source).SelectedValue; 
    if(selectedValue != null){ 
     var selectedColor = (Color)selectedValue; 
    } 
} 
+0

錯誤已解決,我收到了我的itemname,但仍未顯示帶有itemname的矩形 – Jagdish

+0

@Jagdish您真的想讓用戶鍵入ComboBox(可編輯)嗎? –

+0

不,我不想要..我有'矩形'(這是充滿了顏色)和'color_name'除了它在組合框列表中,因爲在代碼中,但我只有當我選擇該項目時color_name。當我選擇一個項目時,我想要兩件事情 – Jagdish

-1

您會看到控件的名稱,因爲組合框不知道從何處獲取所選值的 文本。

相反,只使用其他WPF控件像headeredcontentcontrol代替,你必須一旦選擇了值,該值將被返回的報頭屬性

例如:

 <Grid> 
    <Grid.Resources> 
     <ObjectDataProvider 
ObjectInstance="{x:Type Colors}" 
MethodName="GetProperties" 
x:Key="colorPropertiesOdp" /> 

     <Style TargetType="{x:Type HeaderedContentControl}"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type HeaderedContentControl}"> 
         <Grid> 
          <Grid.ColumnDefinitions> 
           <ColumnDefinition /> 
           <ColumnDefinition /> 
          </Grid.ColumnDefinitions> 
          <ContentPresenter ContentSource="Content" /> 
          <ContentPresenter ContentSource="Header" Grid.Column="1" VerticalAlignment="Center"/> 
         </Grid> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </Grid.Resources> 
    <ComboBox ItemsSource="{Binding Source={StaticResource colorPropertiesOdp}}" Height="23" HorizontalAlignment="Right" Name="comboBox_PC_Opt" VerticalAlignment="Top" Width="130" 
       > 
     <ComboBox.ItemTemplate> 
      <DataTemplate DataType="{x:Type Color}"> 
       <HeaderedContentControl Header="{Binding Path=Name}"> 
        <Rectangle Fill="{Binding Path=Name}" Width="15" Height="15" Margin="0,2,5,2" /> 
       </HeaderedContentControl> 
      </DataTemplate> 
     </ComboBox.ItemTemplate> 
    </ComboBox> 
</Grid> 
+0

它解決了一個錯誤,但是當選擇項目時矩形被禁用 – Jagdish

+0

由於您將組合框的可分割屬性設置爲true,該框將只接受文本。如果你不需要它可編輯刪除此屬性或將其設置爲false,一切都會工作。 – mouradK

0

當您只需添加一個屬性時,代碼太多了。標籤屬性添加到comboboxitem在XAML:

<Comboboxitem Tag="Blue"/> 

然後:

GetValue=ComboboxName.SelectedItem.Tag.ToString() 

的GetValue將 「藍」,而不是 「System.Windows.Controls.ComboBoxItem」