2017-08-23 42 views
0

我有一個組合框使用ComboBoxItems與背景顏色,而不是<System:String>選擇ComboBoxItem使用字符串

enter image description here

<ComboBox x:Name="cboColors" 
      HorizontalAlignment="Right" 
      Margin="0,135,212,0" 
      VerticalAlignment="Top" 
      Width="103"> 
    <ComboBoxItem Background="White" Foreground="Black" Content="White"/> 
    <ComboBoxItem Background="Gray" Foreground="White" Content="Gray"/> 
    <ComboBoxItem Background="#FF262626" Foreground="White" Content="Dark Gray"/> 
    <ComboBoxItem Background="Black" Foreground="White" Content="Black"/> 
    <ComboBoxItem Background="#FFfdfd02" Content="Yellow"/> 
    <ComboBoxItem Background="#FF9aafe4" Content="Blue"/> 
    <ComboBoxItem Background="#FFffb0b0" Content="Pink"/> 
</ComboBox> 

我可以得到ComboBoxItem的價值這樣

ComboBoxItem selectedItem = (ComboBoxItem)(mainwindow.cboColors.SelectedValue); 
string selected = (string)(selectedItem.Content); 

如何使用一組組合框的SelectedItem字符串"Yellow"

cboColors.SelectedItem = "Yellow"; 

ComboBox不會更改。

+0

您需要使用該字符串來標識ItemSource(組合框項目)中的哪個對象是成爲選定項目的對象。即cboColors.SelectedItem = cboColors.Items.FirstOrDefault(item => item.Content.Equals(「Yellow」)); – CodexNZ

+0

@CodexNZ它給出錯誤,「ItemCollection不包含'FirstOrDefault'的定義」。 –

+0

爲什麼你回到硬編碼的XAML對象而不是類來表示組合中的項目?答案在這裏提供:https://stackoverflow.com/questions/45703106/pass-combobox-selected-item-as-method-parameter/45703484#45703484將允許您使用綁定收集作爲您的查找的來源,而不是需要做你正在嘗試的東西。您需要做的就是爲您的ComboItem類添加屬性,以實現後臺和前臺綁定。 – CodexNZ

回答

2

使用this爲出發點:

private void SetSelectedComboBoxItem(string itemName) 
{ 
    ComboItem selected = MyComboItems.FirstOrDefault(i => i.Name.Equals(itemName)); 
    if (selected != null) 
    { 
    combo.SelectedItem = selected; 
    } 
    else 
    { 
    combo.SelectedItem = combo.Items[0]; 
    } 
} 

OR

private void SetSelectedComboBoxItem(string itemName) 
{ 
    ComboItem selected = MyComboItems.FirstOrDefault(i => i.Name.Equals(itemName)); 
    if (selected != null) 
    { 
    SelectedItem = selected; 
    } 
    else 
    { 
    SelectedItem = combo.Items[0]; 
    } 
} 

然後修改您的ComboItem類包括您所使用的着色性能:

public class ComboItem 
{ 
    public string Color { get; private set; } 

    public SolidColorBrush BackgroundColor { get; private set; } 

    public SolidColorBrush ForegroundColor { get; private set; } 

    public ComboItem(string color, SolidColorBrush background, SolidColorBrush foreground) 
    { 
    Color = color; 
    BackgroundColor = background; 
    ForegroundColor = foreground; 
    } 
} 

和變化您的列表初始化以包含新屬性:

List<ComboItem> _myComboItems= new List<ComboItem>() 
    { 
    new ComboItem("White", Brushes.White, Brushes.Black), 
    new ComboItem("Gray", Brushes.Gray, Brushes.White), 
    new ComboItem("Dark Gray", Brushes.DarkGray, Brushes.White), 
    new ComboItem("Black", Brushes.Black, Brushes.White), 
    new ComboItem("Yellow", Brushes.Yellow, Brushes.Black), 
    new ComboItem("Blue", Brushes.Blue, Brushes.Black), 
    new ComboItem("Pink", Brushes.Pink, Brushes.Black) 
    }; 

和修改XAML中已經應用到組合框的樣式類似下面的(這將適用於所有組合框控件在做這樣的應用程序):

<Window.Resources> 
<Style TargetType="{x:Type ComboBoxItem}"> 
    <Setter Property="Template"> 
    <Setter.Value> 
     <ControlTemplate TargetType="{x:Type ComboBoxItem}"> 
     <Border Name="Border" 
       Height="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ComboBox}}, Path=ActualHeight}" 
       Background="{Binding BackgroundColor}" 
       BorderBrush="Transparent"> 
      <Grid> 
      <TextBlock x:Name="ItemText" 
         TextAlignment="Left" 
         VerticalAlignment="Center" 
         Text="{Binding Color}" 
         Margin="5,0,0,0" 
         Foreground="{Binding ForegroundColor}"/> 
      </Grid> 
     </Border> 
     </ControlTemplate> 
    </Setter.Value> 
    </Setter> 
</Style> 

希望我沒有錯過任何東西。

+0

應用顏色工作。我遇到了兩個問題。點擊一個項目不會選擇它,該框保持空白。並且使用'SetSelectedComboBoxItem(「Yellow」)'我得到一個NullReferenceException。以下是我的完整示例代碼:XAML https://pastebin.com/raw/dwxn60VW。 C#https://pastebin.com/raw/494VMfQe。 –

+0

在您的xaml中,您正在使用SelectedItem和SelectedIndex,只使用一個,因爲它們將相互對抗,即使選擇了一個項目,SelectedIndex值也被硬編碼爲0. – CodexNZ

+0

NullReferenceException發生在哪裏?使用你的調試器來找出什麼是空的,並修復它或防止它。 – CodexNZ

1

您應該將SelectedItem設置爲ComboBoxItem而不是string。您可以選擇使用一些LINQ適當ComboBoxItem:

cboColors.SelectedItem = cboColors.Items.OfType<ComboBoxItem>().FirstOrDefault(x => x.Content.ToString() == "Yellow"); 

類型選擇的項目,並在ComboBox的項目必須匹配。