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))
            {
                (sender as ComboBox).Text = "[Selected Table]";
            }
        }

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

4

2 回答 2

1

您可以覆盖ItemTemplateand ItemContainerStyle,这样下拉列表中的 Items 将正确显示,但 ComboBox 中的 Item 将是"[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>

工作副本:

xml:

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

结果:

下拉:已在此处输入图像描述选择:在此处输入图像描述


编辑:

也许这个?

<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));
    }
}
于 2013-03-20T00:45:03.263 回答
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]";
        }
    }
于 2013-03-20T00:21:40.500 回答