【问题标题】:Selecting value from combo dropdown not reflecting从组合下拉列表中选择值不反映
【发布时间】:2014-03-28 10:32:04
【问题描述】:

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

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

Item 1
Item 2
Item 3
Other

现在,当我选择“其他”时,会显示一个对话框来选择资源。在此对话框中,我再次选择“Item1”(下拉列表中的第一项)。现在在 UI 中,它仍将所选项目显示为“其他”。 我调查并发现根本原因是: 在 XAML 中,下拉控件的项目源绑定到名为“ResourceList”的可观察集合。每当在 UI 中选择一个新资源时,它就会添加到此集合中,并且每当我对此集合进行“更改”时,它才会反映在 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>

在此绑定中,“SelectedValue”绑定到视图模型中的“SelectedResource”属性。 在属性“SelectedResource”中,每当它采用新值时,我们都会触发属性更改通知。由于我刚才提到的根本原因,仍然没有反映在 UI 中。 这个问题有什么解决办法吗?

【问题讨论】:

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


    【解决方案1】:

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

    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 以获得更好的解释。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多