使用String选择ComboBoxItem

时间:2017-08-23 00:33:30

标签: c# wpf visual-studio combobox

我有一个使用ComboBoxItems的ComboBox,背景颜色代替<System:String>

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);

如何使用字符串"Yellow" 设置 ComboBox SelectedItem?

cboColors.SelectedItem = "Yellow";

ComboBox不会改变。

2 个答案:

答案 0 :(得分: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以将样式应用于ComboBox,如下所示(这将以这种方式应用于应用程序中的所有组合框控件):

<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>

希望我没有错过任何内容。

答案 1 :(得分:1)

您应该将SelectedItem设置为ComboBoxItem而不是string。您可以使用一些LINQ:

选择适当的ComboBoxItem
cboColors.SelectedItem = cboColors.Items.OfType<ComboBoxItem>().FirstOrDefault(x => x.Content.ToString() == "Yellow");

所选项目的类型和ComboBox中的项目必须匹配。