【问题标题】:Converting a ComboBox to an AutoCompleteBox using the WPF Toolkit使用 WPF 工具包将 ComboBox 转换为 AutoCompleteBox
【发布时间】:2020-12-06 02:10:38
【问题描述】:

我在将“复杂”组合框转换为同样复杂的自动完成框时遇到了一些麻烦。我的目标是能够选择并将 ShoppingCart 的项目设置为类似于列表中的项目之一。这是重现我的情况的三个步骤(我正在使用 Stylet 及其 SetAndNotify() INPC 方法)

  1. 创建两个对象,一个只有一个 Name 属性,另一个只有另一个对象作为属性

    public class ItemModel : PropertyChangedBase
    {
        private string _name;
        public string Name
        {
            get => _name;
            set => SetAndNotify(ref _name, value);
        }
    }
    
    public class ShoppingCartModel : PropertyChangedBase
    {
        public ItemModel Item { get; set; }
    }
    
  2. 初始化并填充 DataContext 中的 ItemsList 和 Shoppingcart(因为我们使用的是 MVVM,所以它是 ViewModel)

    public ShoppingCartModel ShoppingCart { get; set; }
    public ObservableCollection<ItemModel> ItemsList { get; set; }
    
    public ShellViewModel()
    {
        ItemsList = new ObservableCollection<ItemModel>()
        {
            new ItemModel { Name = "T-shirt"},
            new ItemModel { Name = "Jean"},
            new ItemModel { Name = "Boots"},
            new ItemModel { Name = "Hat"},
            new ItemModel { Name = "Jacket"},
        };
    
        ShoppingCart = new ShoppingCartModel() { Item = new ItemModel() };
    }
    
  3. 在 View 中创建 AutoCompleteBox、ComboBox 和一个小 TextBlock 以对其进行全面测试:

    <Window [...] xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=DotNetProjects.Input.Toolkit">
    
        <!-- Required Template to show the names of the Items in the ItemsList -->
        <Window.Resources>
            <DataTemplate x:Key="AutoCompleteBoxItemTemplate">
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Background="Transparent">
                    <Label Content="{Binding Name}"/>
                </StackPanel>
            </DataTemplate>
        </Window.Resources>
    
        <StackPanel>
            <!-- AutoCompleteBox: can see the items list but selecting doesn't change ShoppingCart.Item.Name -->
            <Label Content="AutoCompleteBox with ShoppingCart.Item.Name as SelectedItem:"/>
            <toolkit:AutoCompleteBox ItemsSource="{Binding ItemsList}"
                                     ValueMemberPath="Name"
                                     SelectedItem="{Binding Path=ShoppingCart.Item.Name}" 
                                     ItemTemplate="{StaticResource AutoCompleteBoxItemTemplate}"/>
    
            <!-- ComboBox: can see the items list and selecting changes ShoppingCart.Item.Name value -->
            <Label Content="ComboBox with ShoppingCart.Item.Name as SelectedValue:"/>
            <ComboBox ItemsSource="{Binding ItemsList}" 
                      DisplayMemberPath="Name"
                      SelectedValue="{Binding Path=ShoppingCart.Item.Name}"
                      SelectedValuePath="Name"
                      SelectedIndex="{Binding Path=ShoppingCart.Item}" />
    
            <!-- TextBox: Typing "Jean" or "Jacket" updates the ComboBox, but not the AutoCompleteBox -->
            <Label Content="Value of ShoppingCart.Item.Name:"/>
            <TextBox Text="{Binding Path=ShoppingCart.Item.Name}"/>
        </StackPanel>
    </window>
    

将 AutoCompleteBox 的 SelectedItem 的绑定模式更改为 TwoWay 使其打印“[ProjectName].ItemModel”,这意味着 (我猜?) 它正在获取 ItemModels 而不是字符串,但我似乎无法让它工作。任何帮助将不胜感激,谢谢,如果可以缩小我的帖子,请随时编辑。

【问题讨论】:

    标签: c# wpf xaml wpftoolkit autocompletebox


    【解决方案1】:

    经过多次尝试,终于找到了罪魁祸首:

    • 尽管有PropertyChangedBase 继承,但在ShoppingCartModel.Item 中未实现INPC(实现INPC 或删除PropertyChangedBase 继承工作)

      public class ShoppingCartModel : PropertyChangedBase
      {
          private ItemModel _item;
          public ItemModel Item
          {
              get => _item;
              set => SetAndNotify(ref _item, value);
          }
      }
      
    • AutoCompleteBox 的SelectedItem 必须与ItemsSource 属于同一类型,并且具有TwoWay 模式Binding

      <toolkit:AutoCompleteBox ItemsSource="{Binding ItemsList}"
                               ValueMemberPath="Name"
                               SelectedItem="{Binding Path=ShoppingCart.Item, Mode=TwoWay}" 
                               ItemTemplate="{StaticResource AutoCompleteBoxItemTemplate}"/>
      
    • 最后……最神秘的是组合框!只需在那里,它就会与AutoCompleteBox 混淆,我不知道为什么,只需评论整个 ComboBox 就可以了。如果您知道为什么 ComboBox 会破坏 AutoCompleteBox 的绑定,请随时提供帮助。

    还有另一个问题,在 ListView 中使用 AutoCompleteBox 时,it's better to create a separate question for that issue here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-28
      • 1970-01-01
      • 1970-01-01
      • 2012-08-15
      • 2011-06-01
      • 2015-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多