从组合下拉列表中选择未反映的值

时间:2014-03-28 10:32:04

标签: c# wpf drop-down-menu combobox

我有基于WPF组合的下拉列表,最多可以有4个项目。其中有一个名为"其他"(硬编码)的最后一项允许用户选择新资源。当用户选择新资源时,它会在下拉列表中更新(仍然是4个项目),并相应地根据所选资源,更新UI应用程序中的项目。它还会在UI中显示所选项目。 面临的问题可以描述为:

假设我在下拉列表中有以下资源:

Item 1
Item 2
Item 3
Other

现在,当我选择'其他'时,会显示一个对话框以选择资源。在此对话框中,我再次选择'项目1'(下拉列表中的第一项)。现在,在UI中,它仍会将所选项目显示为'其他'。 我调查并找到了根本原因: 在XAML中,下拉控件的项源被限制为名为" ResourceList"的可观察集合。每当在UI中选择一个新资源时,它就会被添加到这个集合中,每当我做出一个" CHANGE"到这个集合只有它反映在UI否则没有。

XAML代码:

<inputToolkit:UxDropDown x:Name="cmbResourceList"
                         MaxWidth="150"
                         Margin="8 0"
                         HorizontalAlignment="Left"
                         AutomationProperties.AutomationId="cmbResourceListForTask"
                         Cursor="Hand"
                         FontSize="{StaticResource TaskListHeaderFontSize}"
                         FontWeight="Bold"
                         ItemsSource="{Binding ResourceList}"
                         SelectedValue="{Binding Path=SelectedResource,
                                                Mode=TwoWay,
                                                UpdateSourceTrigger=PropertyChanged}"


                         ToolTip="{Binding Path=SelectedResource.DisplayName,
                                           Mode=OneWay}"
                         Visibility="{Binding Path=ShowResourceList,
                                              Converter={StaticResource boolToVisibility}}">
    <inputToolkit:UxDropDown.ItemTemplate>
        <DataTemplate>
            <TextBlock AutomationProperties.AutomationId="SelectedResourceItemForTask"
                       Cursor="Hand"
                       Text="{Binding DisplayName}"
                       TextTrimming="CharacterEllipsis" />
        </DataTemplate>
    </inputToolkit:UxDropDown.ItemTemplate>
</inputToolkit:UxDropDown>

在此绑定&#39; SelectedValue&#39;绑定到“选择资源”&#39;视图模型中的属性。 在物业&#39; SelectedResource&#39;我们在假设一个新值时触发属性更改通知。由于我刚才提到的根本原因,仍未在UI中反映出来。 解决这个问题的方法是什么?

1 个答案:

答案 0 :(得分:0)

让我怀疑你做了这样的事情:

private string _Selection;
    public string Selection
    {
        get { return _Selection; }

        set
        {
            if (_Selection != value)
            {
                if (value == "Other")
                {
                    _Selection = ShowOtherDialog();
                }
                else
                {
                    _Selection = value;
                }

                NotifyPropertyChanged("Selection");
            }
        }
    }

不幸的是,UI不会像这样更新,因为它在将绑定写回源时不会收听通知。

您可能想尝试一下:

private string _Selection;
    public string Selection
    {
        get { return _Selection; }

        set
        {
            if (_Selection != value)
            {
                if (value == "Other")
                {
                    Application.Current.Dispatcher.BeginInvoke(
                        new Action(() =>
                        {
                            _Selection = ShowOtherDialog();
                            NotifyPropertyChanged("Selection");

                       }));                      
                }
                else
                {
                    _Selection = value;
                    NotifyPropertyChanged("Selection");   
                }


            }
        }
    }

您可以阅读this article以获得更好的解释。

相关问题