繁体   English   中英

UWP 中 ComboBox 的奇怪行为 - SelectedItem & ItemsSource

[英]Curious behavior with ComboBox in UWP - SelectedItem & ItemsSource

希望你们一切都好。 我在 UWP 应用程序中遇到了 ComboBox 的 2 个问题。

  1. 如果属性ItemsSource绑定到实现INotifyPropertyCollectionChanged的集合,则永远不会完全加载列表。 我只有 2、3 或 4 个第一个项目……取决于时间。 当同一个集合绑定到 DataGrid 时没问题,所以我认为我的集合是正确构建的。 作为一种解决方法(代码隐藏),我首先加载我的集合(在任务中)并在任务完成时设置 ItemsSource 属性。 该解决方案有效,但我想在代码隐藏中做更少的事情。
  2. 属性 SelectedItem 上的绑定似乎仅适用于 ReferenceEquals,我的集合中的项目类型基于 ID 实现 Equals,并且已在控制台应用程序中单独并成功地进行了测试。 作为一种解决方法(代码隐藏),一旦我的列表被加载,我改变绑定到 SelectedItem 的属性,如下所示:
 Users.TaskFill.ContinueWith(t => BaseItemCollection.UserInterfaceAction.Invoke(() => { if (Item?.Manager.= null) Item.Manager = t.Result.FirstOrDefault(i => i;Equals(Manager)). ComboBoxManager.SetBinding(ComboBox,ItemsSourceProperty, this, "Users". BindingMode;TwoWay). ComboBoxManager.SetBinding(Selector,SelectedItemProperty, "Manager". BindingMode;TwoWay); }));
  • 用户是我的集合(异步填充),用作 ComboBox 的源
  • SetBinding是我自己创建的一种自定义扩展方法,用于从单行代码隐藏设置绑定(如下所示):
 public static class ExtensionMethods { #region DependencyObject public static void SetBinding(this DependencyObject dependencyObject, DependencyProperty dependencyProperty, object source, string propertyName, BindingMode mode) { var binding = new Binding() { Source = source, Path = new PropertyPath(propertyName), Mode = mode }; BindingOperations.SetBinding(dependencyObject, dependencyProperty, binding); } public static void SetBinding(this DependencyObject dependencyObject, DependencyProperty dependencyProperty, string propertyName, BindingMode mode) { var binding = new Binding() { Path = new PropertyPath(propertyName), Mode = mode }; BindingOperations.SetBinding(dependencyObject, dependencyProperty, binding); } #endregion }

如何在不需要这些解决方法的情况下从 XAML 得到这个工作? 多年来,我一直能够获得与 WPF 一起使用的类似配置,但在 UWP 上真的很挣扎......

预先感谢您的帮助。

如果属性 ItemsSource 绑定到实现 INotifyPropertyCollectionChanged 的集合,则永远不会完全加载该列表。

如果您的集合不是字符串,则需要指定DisplayMemberPath ,请检查以下代码。 并请检查收藏是否有价值。 对于我实现INotifyPropertyCollectionChanged的测试集合,它适用于ComboBox

<ComboBox
    x:Name="cmbCountry"
    Grid.Row="4"
    Width="292"
    Height="32"
    Margin="28,0,0,0"
    HorizontalAlignment="Left"
    VerticalAlignment="Center"
    DisplayMemberPath="FirstName"
    ItemsSource="{Binding MyItems}"
    PlaceholderText="Select Country ..."
    />

ComboBox 的奇怪行为

ComboBox ItemsPanelTemplate CuriousPanel,可以在触摸设备中实现滚动循环。 如果您不想使用它,可以将其替换为 StackPanel

<ComboBox.ItemsPanel>
    <ItemsPanelTemplate>
        <StackPanel />
    </ItemsPanelTemplate>
</ComboBox.ItemsPanel>
<ComboBox.ItemTemplate>

属性 SelectedItem 上的绑定似乎仅适用于 ReferenceEquals,

SelectedItem不是ComboBox显示字段,它是一个完整的 User object。 您可以在SelectedItem绑定属性设置方法中获取 select 用户。 以下是您可以参考的完整代码。

public sealed partial class TestPage : Page, INotifyPropertyChanged
{
    private User _selecteduser;

    public TestPage()
    {
        this.InitializeComponent();

        _myItems = new ObservableCollection<User>
        {
            new User{UserId=1,FirstName="Fay",LastName="Wang",City="Delhi",State="DEL",Country="INDIA"},
            new User{UserId=2,FirstName="Mark",LastName="Liu",City="New York", State="NY", Country="USA"},
            new User{UserId=3,FirstName="Rich",LastName="Cai",City="Philadelphia", State="PHL", Country="USA"},
            new User{UserId=4,FirstName="Eveia",LastName="Dong",City="Noida", State="UP", Country="CANADA"}}
        };

        this.DataContext = this;
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)//string propertyName
    {
        if (PropertyChanged != null)

        {
            PropertyChangedEventArgs args = new PropertyChangedEventArgs(propertyName);
            this.PropertyChanged(this, args);
        }
    }

    public ObservableCollection<User> Users
    {
        get
        { return _myItems; }
        set
        {
            _myItems = value;
            OnPropertyChanged("Users");
        }
    }
    private ObservableCollection<User> _myItems;

    public User SelectedUser
    {
        get
        {
            return _selecteduser;
        }
        set
        {
            _selecteduser = value;


            OnPropertyChanged("SelectedUser");
        }
    }
}

Xaml

<ComboBox
    x:Name="cmbCountry"
    Grid.Row="4"
    Width="292"
    Height="32"
    Margin="28,0,0,0"
    HorizontalAlignment="Left"
    VerticalAlignment="Center"
    DisplayMemberPath="FirstName"
    ItemsSource="{Binding Users}"
    PlaceholderText="Select User..."
    SelectedItem="{Binding SelectedUser, Mode=TwoWay}"
    />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM