WPF组合框 - 按标签选择项目

时间:2011-10-06 13:14:12

标签: c# wpf xaml combobox

我有一个像这样的组合框

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

如何通过标记值以编程方式设置所选索引?例如。 &#39; myMenu.selectedTag = 3&#39;和问题3将是选定的项目?

我想要的东西比现在的解决方案更容易......

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

3 个答案:

答案 0 :(得分:3)

看起来你正在寻找ComboBox控件的proeprty SelectedValuePath。请参阅此处示例http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.selector.selectedvaluepath.aspx

对于那些在上面的链接中找不到示例并且继续保持低调的人,我准备了自己的例子。它重点介绍如何设置ComboBox并通过指定选定的值来选择适当的项目。

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;

    }
}

答案 1 :(得分:0)

你应该使用数据绑定这样的东西,但你可以使用LINQ:

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

答案 2 :(得分:0)

您可以将选定的combox值绑定到依赖项属性。例如,这是一个具有依赖属性“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); }
    }
}

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

然后要更改所选项目,只需修改属性的值,如上例所示(CurrentTag =“4”;)