如何使用不同的文本更新ComboBox文本框

时间:2013-03-19 23:51:28

标签: c# .net wpf events user-interface

我有一个可编辑的ComboBox。当用户选择一个项目时,我想覆盖文本框中的文本,使其与选择的值不同。例如,无论用户选择什么,我希望以下内容显示在可编辑组合框的文本框中:“[Selected Table]”。这是我到目前为止所尝试的:

private void cmbValue1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBoxItem selection = (sender as ComboBox).SelectedItem as ComboBoxItem;

            //If the tag is a TableContainer, then the user has selected a sub-query.
            if (selection != null && selection.Tag != null && selection.Tag.GetType() == typeof(TableContainer))
            {
                (sender as ComboBox).Text = "[Selected Table]";
            }
        }

正在显示用户的实际选择值,所以我认为一旦编译器完成我的方法实现,它就会转到其他地方并将文本更改为所选项目的文本。

2 个答案:

答案 0 :(得分:1)

您可以覆盖ItemTemplateItemContainerStyle,这样下拉菜单中的项目会正确显示,但ComboBox中的项目将为"[Selected Table]"

示例:

 <ComboBox ItemsSource="{Binding Items}">
    <ComboBox.ItemTemplate>
        <!-- The template for the SelectedItem -->
        <DataTemplate>
            <TextBlock Text="[Selected Table]" />
        </DataTemplate>
    </ComboBox.ItemTemplate>
    <ComboBox.ItemContainerStyle>
        <!-- The style for the dropdown items -->
        <Style TargetType="{x:Type ComboBoxItem}">
            <Setter Property="Template">
                <Setter.Value>
                     <!-- Add dataTemplate here if you are binding to complex object -->
                    <ControlTemplate TargetType="{x:Type ContentControl}">
                        <TextBlock Text="{Binding}"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ComboBox.ItemContainerStyle>
</ComboBox>

工作副本:

的Xaml:

<Window x:Class="WpfApplication9.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="132" Width="278" Name="UI">

    <StackPanel DataContext="{Binding ElementName=UI}">
        <ComboBox ItemsSource="{Binding Items}">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="[Selected Table]" />
                </DataTemplate>
            </ComboBox.ItemTemplate>
            <ComboBox.ItemContainerStyle>
                <Style TargetType="{x:Type ComboBoxItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ContentControl}">
                                <TextBlock Text="{Binding}"/>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ComboBox.ItemContainerStyle>
        </ComboBox>
    </StackPanel>
</Window>

代码:

public partial class MainWindow : Window
{
    public MainWindow() 
    {
        InitializeComponent();
        Items.Add("Item1");
        Items.Add("Item2");
        Items.Add("Item3");
        Items.Add("Item4");
    }

    private ObservableCollection<string> _items = new ObservableCollection<string>();
    public ObservableCollection<string> Items
    {
        get { return _items; }
        set { _items = value; }
    }
}

结果:

DropDown:enter image description here已选中:enter image description here


编辑:

也许这个?

<Window x:Class="WpfApplication17.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication17"
        Title="MainWindow" Height="350" Width="500" Name="UI">
    <StackPanel DataContext="{Binding ElementName=UI}">
        <ComboBox x:Name="combo" ItemsSource="{Binding Items}">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock>
                        <TextBlock.Style>
                            <Style TargetType="TextBlock">
                                <Setter Property="Text" Value="{Binding}" />
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding IsDropDownOpen, ElementName=combo}" Value="false">
                                        <Setter Property="Text" Value="{Binding MyText, ElementName=UI}" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </TextBlock.Style>
                    </TextBlock>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
    </StackPanel>

代码:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    private string _myText;
    private ObservableCollection<string> _items = new ObservableCollection<string>();

    public MainWindow()
    {
        InitializeComponent();
        Items.Add("Item1");
        Items.Add("Item2");
        Items.Add("Item3");
        Items.Add("Item4");
        MyText = "StackOverflow";
    }

    public string MyText
    {
        get { return _myText; }
        set { _myText = value; OnPropertyChanged("MyText"); }
    }

    public ObservableCollection<string> Items
    {
        get { return _items; }
        set { _items = value; }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string e)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(e));
    }
}

答案 1 :(得分:0)

只能在问题中提出要求的方法是删除事件,然后再将其添加回来。虽然没有覆盖类并自己构建此功能或使用替代方法,但无法阻止事件触发。

    private void cmbValue1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        ComboBoxItem selection = (sender as ComboBox).SelectedItem as ComboBoxItem;

        //If the tag is a TableContainer, then the user has selected a sub-query.
        if (selection != null && selection.Tag != null && selection.Tag.GetType() == typeof(TableContainer))
        { 
            //This is the only way to disable an event action
            (sender as ComboBox).TextChanged -= sender_OnTextChanged; 
            (sender as ComboBox).Text = "[Selected Table]";
            (sender as ComboBox).TextChanged += sender_OnTextChanged; 

            //But as said above though, you could also do this:
            (sender as ComboBox).SelectedText = "[Selected Table]";
        }
    }