【问题标题】:Editable Combo box loses selected value when the item source is changed更改项目源时,可编辑组合框丢失选定值
【发布时间】:2014-11-11 13:30:06
【问题描述】:

我有一个可编辑的组合框,如果更改了项目源以使当前选定的项目从项目源中删除,则该组合框会丢失值。

代码如下,

<ComboBox x:Name="TableNameCombo"
          MinWidth="100"
          IsEditable="True"
          ItemsSource="{Binding TableNames}"
          SelectionChanged="TableNameCombo_SelectionChanged"
          Text="{Binding TableName,
                 ValidatesOnDataErrors=True,
                 UpdateSourceTrigger=PropertyChanged}" />

如果我在更改项目源时处于不同的视图中,则保留该值。 仅当带有组合框的视图处于活动状态时更改 itemsource 时,该值才会丢失。

请帮助我如何保留组合框值,即使它不存在于 itemsource 中,并且当包含组合框的视图处于活动状态时项目源发生更改

注意:

1.通过视图我的意思是我有一个选项卡式面板并且有不同的视图 选项卡。

2.我不是在谈论任何后备价值。我只想 保留任何选定的值,即使它不存在于 组合框项目来源。

让我把这个问题澄清一下非常简单的要求, 这是我的示例应用程序的屏幕截图,

当用户在文本框中输入一个项目并单击删除项目按钮时,该项目将从集合中删除,该集合是 Combobox 的 itemSource。但是当我这样做时,该项目不会显示在组合框中。

我的要求是即使该项目不在集合中,仍将其保留在组合框中。

【问题讨论】:

  • 这听起来像是一种 hack,而不是您打算做的事情的解决方案。你的目标是什么?
  • 我的目标是在组合框中保留最后选择的值,即使该值已从设置为项目源的集合中删除。
  • 嗨@ScottNimrod。我希望现在这个问题很容易理解。

标签: c# wpf combobox


【解决方案1】:

您可以添加此代码:

using System.Collections;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

public static class ComboBoxItemsSourceDecorator
{
    public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.RegisterAttached(
        "ItemsSource",
        typeof(IEnumerable),
        typeof(ComboBoxItemsSourceDecorator),
        new PropertyMetadata(null, ItemsSourcePropertyChanged));

    public static void SetItemsSource(UIElement element, bool value)
    {
        element.SetValue(ItemsSourceProperty, value);
    }

    public static IEnumerable GetItemsSource(UIElement element)
    {
        return (IEnumerable)element.GetValue(ItemsSourceProperty);
    }

    private static void ItemsSourcePropertyChanged(DependencyObject element, DependencyPropertyChangedEventArgs e)
    {
        var target = element as ComboBox;
        if (element == null)
        {
            return;
        }

        // Save original binding 
        var originalBinding = BindingOperations.GetBinding(target, ComboBox.TextProperty);

        BindingOperations.ClearBinding(target, ComboBox.TextProperty);
        try
        {
            target.ItemsSource = e.NewValue as IEnumerable;
        }
        finally
        {
            if (originalBinding != null)
            {
                BindingOperations.SetBinding(target, ComboBox.TextProperty, originalBinding);
            }
        }
    }
}

并像这样使用:

<ComboBox IsTextSearchEnabled="true" 
    IsTextSearchCaseSensitive="false"
    options:ComboBoxItemsSourceDecorator.ItemsSource="{Binding Path=Values, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}"                      
    Text="{Binding Path = Value}"
    IsEditable ="True">

它只是在 ItemsSouce 更改时基本上保留对相同文本的绑定。

注意:使用装饰器 ItemsSource,而不是内置装饰器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-17
    • 1970-01-01
    • 2012-05-23
    • 2015-09-30
    相关资源
    最近更新 更多