[英]Curious behavior with ComboBox in UWP - SelectedItem & ItemsSource
希望你们一切都好。 我在 UWP 应用程序中遇到了 ComboBox 的 2 个问题。
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); }));
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.