0

我有一个用户可编辑的组合框,因此我将 Text 属性绑定到我的类的属性。同一个组合框的 ItemsSource 绑定到 AsyncObservableCollection (我根据其他帖子做了,效果很好)。

但是,我在更新 ItemsSource 时遇到了问题。

以下是重现的步骤:

  1. 在组合框下拉列表中选择一个值。
  2. 在组合框中键入一些文本。(说“aaa”)
  3. 更新 ItemsSource。(通过我的按钮点击)

结果:MyText 属性仍然设置为您键入的文本(“aaa”),但组合框显示一个空白条目。

但是,如果您执行上述相同步骤但跳过第 1 步,则组合框会正确显示 MyText 属性中的文本。这使我相信在对 ItemsSource 的更新完成后,所选索引/所选值正在用于更新组合框。

关于在更新 ItemsSource 后如何使显示的值与 MyText 属性同步的任何想法?

在下面提供的代码中,我正在更新按钮单击上的 ItemsSource 以便重现。

谢谢你!

XAML:

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
    <ComboBox Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="comboBox1" VerticalAlignment="Top" Width="200" IsEditable="True"
                          DataContext="{Binding Path=MyDataClass}"
                          ItemsSource="{Binding Path=MyListOptions}"
                          SelectedIndex="{Binding Path=MySelectedIndex}"
                          Text="{Binding Path=MyText, UpdateSourceTrigger=LostFocus}"
              >
    </ComboBox>
    <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="416,276,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>

后面的代码:

using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Diagnostics;

namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public class DataClass : INotifyPropertyChanged
        {
            private string mytext = "";
            public string MyText
            {
                get
                {
                    return mytext;
                }
                set
                {
                    mytext = value;
                    OnPropertyChanged("MyText");
                }
            }

            private int myselectedindex = -1;
            public int MySelectedIndex 
            {
                get
                {
                    return myselectedindex;
                }

                set
                {
                    if (value != -1)
                    {
                        mytext = MyListOptions[value];
                        OnPropertyChanged("MyText");
                    }
                }
            }

            private AsyncObservableCollection<string> mylistOptions = new AsyncObservableCollection<string>();
            public AsyncObservableCollection<string> MyListOptions
            {
                get
                {
                    return mylistOptions;
                }

                set
                {
                    mylistOptions.Clear();
                    OnPropertyChanged("MyListOptions");
                    foreach (string opt in value)
                    {
                        mylistOptions.Add(opt);
                    }
                    OnPropertyChanged("MyListOptions");
                }
            }

            public DataClass()
            {
            }

            public event PropertyChangedEventHandler PropertyChanged;

            internal void OnPropertyChanged(string prop)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(prop));
            }
        }

        public DataClass MyDataClass { get; set; }

        public MainWindow()
        {
            MyDataClass = new DataClass();

            MyDataClass.MyListOptions.Add("Option 1 - Provides helpful stuff.");
            MyDataClass.MyListOptions.Add("Option 2 - Provides more helpful stuff.");
            MyDataClass.MyListOptions.Add("Option 3 - Provides extra helpful stuff.");

            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            AsyncObservableCollection<string> newList = new AsyncObservableCollection<string>();
            newList.Add("Option A - Provides helpful stuff.");
            newList.Add("Option B - Provides more helpful stuff.");
            newList.Add("Option C - Provides extra helpful stuff.");

            MyDataClass.MyListOptions = newList;

        }
    }
}
4

1 回答 1

0

好的,我通过将 SelectedValue 绑定到与 Text 相同的属性并将其模式设置为 OneWay 解决了这个问题。

    <ComboBox Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="comboBox1" VerticalAlignment="Top" Width="200" IsEditable="True"
                          DataContext="{Binding Path=MyDataClass}"
                          ItemsSource="{Binding Path=MyListOptions}"
                          SelectedIndex="{Binding Path=MySelectedIndex}"
                          SelectedValue="{Binding Path=MyText, Mode=OneWay}"
                          Text="{Binding Path=MyText, UpdateSourceTrigger=LostFocus}"
于 2012-10-16T20:22:49.857 回答