簡體   English   中英

使用字符串選擇ComboBoxItem

[英]Select ComboBoxItem Using String

我有一個ComboBox使用ComboBoxItems和背景色,而不是<System:String>

在此處輸入圖片說明

<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不會更改。

以此為起點:

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];
  }
}

要么

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的樣式,如下所示(這將應用於以這種方式完成的應用程序中的所有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>

希望我沒有錯過任何事情。

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

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

所選項目的類型和ComboBox的項目必須匹配。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM