WPF Combo box - select an 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? For instance. 'myMenu.selectedTag = 3' and question 3 is the selected item?

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

  int tagToSelect = 3; foreach (ComboBoxItem item in myMenu.Items) { if(item.Tag.Equals(tagToSelect) { myMenu.SelectedItem = item; } } 
+7
source share
3 answers

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

For those people who canโ€™t find an example from the link above and continue to lower the vote, I prepared my own example. It tells you how to configure ComboBox and select the appropriate item by assigning the 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; } } 
+3
source

You should use data binding for something like this, but you can do it with LINQ:

 int tagToSelect = 3; myMenu.SelectedItem = myMenu.Items.Single(t => t.Tag.Equals(tagToSelect)); 
0
source

You can bind the selected combox value to a dependency property. For example, here is a window with the dependency property "CurrentTag":

 public partial class Window1 : Window { public Window1() { InitializeComponent(); } public override void OnApplyTemplate() { base.OnApplyTemplate(); CurrentTag = "4"; } public static readonly DependencyProperty CurrentTagProperty = DependencyProperty.Register( "CurrentTag", typeof(string), typeof(Window1), new PropertyMetadata("1")); public string CurrentTag { get { return (string)this.GetValue(CurrentTagProperty); } set { this.SetValue(CurrentTagProperty, value); } } } 

and in xaml:

 <Window x:Class="WpfComboboxBinding.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="100" Width="300" x:Name="window1"> <StackPanel VerticalAlignment="Center"> <ComboBox Name="myMenu" SelectedValue="{Binding ElementName=window1, Path=CurrentTag, Mode=TwoWay}" SelectedValuePath="Tag"> <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> </StackPanel> </Window> 

Then, to change the selected item, you simply change the value of the property, as in the above example (CurrentTag = "4";)

0
source

Source: https://habr.com/ru/post/898765/


All Articles