英文:
WinUI 3 Show selected items from ListView
问题
I have a WinUI 3 project scaffolded using Template Studio. I have a list view populated with an Enum. I want to show my selected items in another list, but the binding does not work.
public IEnumerable<KeyValuePair<string, string>> ValidationFlagsList => EnumExtensions.GetAllValuesAndDescriptions<ValidationFlag>();
//...
public static IEnumerable<KeyValuePair<string, string>> GetAllValuesAndDescriptions<TEnum>() where TEnum : struct, IConvertible, IComparable, IFormattable
{
return typeof(TEnum).IsEnum ? (from e in Enum.GetValues(typeof(TEnum)).Cast<Enum>() select new KeyValuePair<string, string>(e.ToString(), e.GetDescription())) : throw new ArgumentException("TEnum must be an Enumeration type");
}
<ListView
x:Name="FlagsListView"
SelectionMode="Multiple"
ItemsSource="{x:Bind ViewModel.ValidationFlagsList, Mode=OneTime}"
SelectedValuePath="Key"
DisplayMemberPath="Value">
</ListView>
In another part of xaml I want to show the selected items. I tried two variants:
1.
<ListView ItemsSource="{Binding SelectedItems, ElementName=FlagsListView, Mode=OneWay}"/>
2.
<StackPanel DataContext="{Binding SelectedItems, ElementName=FlagsListView}">
<TextBlock Text="{Binding}"/>
</StackPanel>
Nothing shows on UI. How can I bind correctly?
Is it because IEnumerable is static and ObservableCollection is needed? But the xaml ListView should give me some straightforward binding. Documentation points to this Data binding. I read about creating a class with IsSelected property, but I only need a readonly list, preferably to add something only in xaml.
英文:
I have a WinUI 3 project scaffolded using Template Studio. I have a list view populated with an Enum. I want to show my selected items in another list, but the binding does not work.
Populated with Enum meaning I take <key, value> pairs with enum value and enum description and use as ItemsSource. Selection Mode Multiple active.
public IEnumerable<KeyValuePair<string, string>> ValidationFlagsList => EnumExtensions.GetAllValuesAndDescriptions<ValidationFlag>();
//...
public static IEnumerable<KeyValuePair<string, string>> GetAllValuesAndDescriptions<TEnum>() where TEnum : struct, IConvertible, IComparable, IFormattable
{
return typeof(TEnum).IsEnum ? (from e in Enum.GetValues(typeof(TEnum)).Cast<Enum>() select new KeyValuePair<string, string>(e.ToString(), e.GetDescription())) : throw new ArgumentException("TEnum must be an Enumeration type");
}
<ListView
x:Name="FlagsListView"
SelectionMode="Multiple"
ItemsSource="{x:Bind ViewModel.ValidationFlagsList, Mode=OneTime}"
SelectedValuePath="Key"
DisplayMemberPath="Value">
</ListView>
In another part of xaml I want to show the selected items. I tried two variants:
1.
<ListView ItemsSource="{Binding SelectedItems, ElementName=FlagsListView, Mode=OneWay}"/>
2.
<StackPanel DataContext="{Binding SelectedItems, ElementName=FlagsListView}">
<TextBlock Text="{Binding}"/>
</StackPanel>
Nothing shows on UI. How can I bind correctly?
Is it because IEnumerable is static and ObservableCollection is needed? But the xaml ListView should give me some straightforward binding. Documentation points to this Data binding. I read about creating a class with IsSelected property, but I only need a readonly list, preferably to add something only in xaml.
答案1
得分: 1
ListView具有SelectedItems属性,但它只是一个普通属性,而不是DependencyProperty
。不幸的是,你无法在绑定中使用它。
你可以创建一个自定义的ListView:
ListViewEx.cs
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System.Collections;
using System.Collections.Generic;
namespace ListViews
{
public class ListViewEx : ListView
{
public ListViewEx() : base()
{
this.SelectionChanged += ListViewEx_SelectionChanged;
}
public new IList SelectedItems
{
get => (IList)GetValue(SelectedItemsProperty);
set => SetValue(SelectedItemsProperty, value);
}
public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register(
nameof(SelectedItems),
typeof(IList),
typeof(ListViewEx),
new PropertyMetadata(new ObservableCollection<object>()));
private void ListViewEx_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
foreach (object item in e.RemovedItems)
{
SelectedItems.Remove(item);
}
foreach (object item in e.AddedItems)
{
SelectedItems.Add(item);
}
}
}
}
并像这样使用它:
<Grid ColumnDefinitions="*,*">
<local:ListViewEx
x:Name="FlagsListView"
Grid.Column="0"
DisplayMemberPath="Value"
ItemsSource="{x:Bind ViewModel.ValidationFlagsList, Mode=OneTime}"
SelectedValuePath="Key"
SelectionMode="Multiple" />
<ListView
Grid.Column="1"
ItemsSource="{x:Bind FlagsListView.SelectedItems, Mode=OneWay}" />
</Grid>
英文:
The ListView does have a SelectedItems property but it's just a plain property and not a DependencyProperty
. Unfortunately you can't use it with bindings.
What you can do is create a custom ListView
:
ListViewEx.cs
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System.Collections;
using System.Collections.Generic;
namespace ListViews;
public class ListViewEx : ListView
{
public ListViewEx() : base()
{
this.SelectionChanged += ListViewEx_SelectionChanged;
}
public new IList SelectedItems
{
get => (IList)GetValue(SelectedItemsProperty);
set => SetValue(SelectedItemsProperty, value);
}
public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register(
nameof(SelectedItems),
typeof(IList),
typeof(ListViewEx),
new PropertyMetadata(new ObservableCollection<object>()));
private void ListViewEx_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
foreach (object item in e.RemovedItems)
{
SelectedItems.Remove(item);
}
foreach (object item in e.AddedItems)
{
SelectedItems.Add(item);
}
}
}
and use it like this:
<Grid ColumnDefinitions="*,*">
<local:ListViewEx
x:Name="FlagsListView"
Grid.Column="0"
DisplayMemberPath="Value"
ItemsSource="{x:Bind ViewModel.ValidationFlagsList, Mode=OneTime}"
SelectedValuePath="Key"
SelectionMode="Multiple" />
<ListView
Grid.Column="1"
ItemsSource="{x:Bind FlagsListView.SelectedItems, Mode=OneWay}" />
</Grid>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论