繁体   English   中英

选择特定的ComboBoxItem时,如何将可编辑的ComboBox的文本更改为其他内容?

[英]How to change the editable ComboBox's text to something else when a specific ComboBoxItem is selected?

新文本将永远不会出现在ComboBox内的项目中。 下面是完整的XAML +代码隐藏,无法正常运行。 我要达到的目的是让用户从组合框中选择一个实际的项目组,或者将一个应作为空字符串应用到ComboBox内的文本框中的No Group (斜体,变灰)项目。

我也尝试过:

  1. StaysOpenOnEdit="True" (具有相同的结果)和
  2. 通过处理"click me" ComboBoxItemSelected事件(在更改ComboBox的TextSelectedItem属性之前调用事件处理程序)。

XAML

<Window x:Class="cs_wpf_test_12.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"
        xmlns:local="clr-namespace:cs_wpf_test_12"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel Orientation="Vertical">
        <ComboBox SelectionChanged="ComboBox_SelectionChanged"
                  IsEditable="True">
            <ComboBoxItem>test</ComboBoxItem>
            <ComboBoxItem Foreground="Gray">click me</ComboBoxItem>
        </ComboBox>
    </StackPanel>
</Window>

代码隐藏

internal bool HandlingSelectionChange = false;
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (HandlingSelectionChange)
    {
        return;
    }
    HandlingSelectionChange = true;
    var cb = sender as ComboBox;
    if (cb.Text == "click me")
    {
        cb.Text = "";
        e.Handled = true;
    }
    HandlingSelectionChange = false;
}

预期:当用户单击下拉菜单中的“单击我”项时, ComboBox的文本将变为空字符串。 单击其余项目时,应将其文本正常复制到ComboBox的文本框中。

实际:

  1. 启动程序。
  2. 选择“单击我”项。
  3. 文本更改为“单击我”(未显示为灰色),而不是“”。
  4. 点击“测试”项。
  5. 文本更改为“”(空字符串),而不是“ test”。
  6. 再次单击“测试”项。
  7. 文本更改为“测试”。

更新

我希望使用MVVM,但我仍然是初学者。 我有几个ComboBox ES样上方的内部示出DataGridTemplateColumn ,并且对于每个所述的ComboBox ES(其应具有相同的下拉内容)我想应该有一个ViewModel其每个ComboBoxItem秒。 如果可能,我想学习在这种情况下如何正确使用MVVM。

大XAML

<DataGridTemplateColumn Header="Group Name">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Label Content="{Binding GroupName, Mode=OneWay}">
            </Label>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <ComboBox IsEditable="True" StaysOpenOnEdit="True"
                        ItemsSource="{Binding Path=Clocks.GroupsVM,
                RelativeSource={RelativeSource AncestorType=local:ClockDataGrid}}"
                        PreviewKeyDown="ComboBox_PreviewKeyDown"
                        SelectionChanged="ComboBox_SelectionChanged"
                        Text="{Binding GroupName}">
                <ComboBox.Resources>
                    <Style TargetType="ComboBoxItem">
                        <Setter Property="FontStyle" Value="{Binding FontStyle}"/>
                        <Setter Property="Foreground" Value="{Binding Foreground}"/>
                    </Style>
                </ComboBox.Resources>
            </ComboBox>
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

背后的大代码

private void ComboBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
    var cb = sender as ComboBox;

    if ((e.Key == Key.Return ||
        e.Key == Key.Enter) &&
        cb.Text != "")
    {
        bool duplicate = false;
        foreach (ClockGroupVM vm in Clocks.GroupsVM)
        {
            if (vm.Name == cb.Text)
            {
                cb.SelectedItem = vm;
                duplicate = true;
                break;
            }
        }

        if (duplicate)
        {
            return;
        }

        // create a ClockGroupM and corresponding ClockGroupVM
        // (ClockGroupVM inherits from ClockGroupM)
        var cvm = new ClockGroupVM()
        {
            Name = cb.Text
        };
        Clocks.Groups.Insert(0, cvm);
        cb.SelectedItem = cvm;
    }
}

internal bool HandlingSelectionChange = false;
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (HandlingSelectionChange)
    {
        return;
    }
    HandlingSelectionChange = true;

    var cb = sender as ComboBox;

    //if (cb.SelectedItem is the VM with Style != Normal)

    ClockGroupVM foundVM = null;
    foreach (ClockGroupVM vm in Clocks.GroupsVM)
    {
        if (vm.FontStyle != FontStyles.Normal &&
            ReferenceEquals(cb.SelectedItem, vm))
        {
            foundVM = vm;
            break;
        }
    }

    if (foundVM != null)
    {
        cb.Text = "";
        e.Handled = true;
    }

    HandlingSelectionChange = false;
}

ComboBox.Text不会在SelectionChanged事件中立即更新。 您可以改用SelectedValue的内容。 并将SelectedValue设置为null。 像这样更改您的if条件。

if ((cb.SelectedValue as ComboBoxItem).Content.ToString() == "click me")
{
    cb.Text = "";
    cb.SelectedValue = null;
    e.Handled = true;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM