Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Combo box - Select Item by Tag

I have a combo box like this

    <ComboBox Name="myMenu">
        <ComboBoxItem Content="Question 1" Tag="1"  />
        <ComboBoxItem Content="Question 2" Tag="2"  />
        <ComboBoxItem Content="Question 3" Tag="3"  />
        <ComboBoxItem Content="Question 4" Tag="4"  />
    </ComboBox>

How can I programmatically set the selected index by Tag Value? E.g. 'myMenu.selectedTag = 3' and Question 3 would be the selected item?

I want something easier than my current solution really...

      int tagToSelect = 3;
      foreach (ComboBoxItem item in myMenu.Items)
      {
          if(item.Tag.Equals(tagToSelect)
          {
               myMenu.SelectedItem = item;
          }
      }
like image 503
Eddie Avatar asked Oct 06 '11 13:10

Eddie


1 Answers

Looks like you're looking for the proeprty SelectedValuePath of ComboBox control. See example here http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.selector.selectedvaluepath.aspx

For those people who cannot find example under the link above and keep downvoting I prepared my own example. It highlights how to setup ComboBox and select appropriate item by assigning selected value.

MainWindow.xaml

<Window x:Class="WpfApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" Height="97" Width="202">
<Window.Resources>
    <XmlDataProvider x:Key="Questions" XPath="/Questions/*">
        <x:XData>
            <Questions xmlns="">
                <Question Title="Question 1" Index="1" />
                <Question Title="Question 2" Index="2" />
                <Question Title="Question 3" Index="3" />
                <Question Title="Question 4" Index="4" />
            </Questions>
        </x:XData>
    </XmlDataProvider>
    <DataTemplate x:Key="QuestionItemTemplate">
        <TextBlock Text="{Binding XPath=@Title}" />
    </DataTemplate>
</Window.Resources>
<StackPanel>
    <ComboBox Name="myMenu"
              ItemsSource="{Binding Source={StaticResource Questions}}"
              ItemTemplate="{StaticResource QuestionItemTemplate}"
              SelectedValuePath="@Index"/>
    <TextBlock Text="{Binding ElementName=myMenu, 
        Path=SelectedValue, StringFormat=Selected Index: {0:D}}"/>
    <Button Content="Select another item" Click="Button_Click" />
</StackPanel>

MainWindow.xaml.cs

using System.Windows;

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

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            myMenu.SelectedValue = ++_counter;
            if (_counter > 3) _counter = 0;
        }

        private int _counter = 0;

    }
}
like image 80
Sergei B. Avatar answered Sep 30 '22 13:09

Sergei B.