WinUI中DataGrid选中项数量绑定至可见性失效问题求助
WinUI DataGrid选中项数量绑定控件可见性失效的解决办法
WinUI里DataGrid.SelectedItems.Count的绑定不会在选中项变化时自动更新,这是因为SelectedItems的Count属性不会触发属性更改通知,导致转换器只在初始化时执行一次,后续选中项变化时绑定目标不会更新。下面是几种可行的解决方法:
方法一:后台代码监听SelectionChanged事件
直接在后台处理选中变化事件,手动更新控件可见性:
- XAML中给DataGrid添加事件:
<DataGrid x:Name="MyDataGrid" SelectionChanged="MyDataGrid_SelectionChanged" />
- 后台实现事件处理:
private void MyDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e) { // TargetControl是你要控制可见性的控件名称 TargetControl.Visibility = MyDataGrid.SelectedItems.Count == 1 ? Visibility.Visible : Visibility.Collapsed; }
方法二:用附加属性封装选中计数(适配MVVM)
创建一个附加属性,自动监听DataGrid的选中变化并更新计数,这样就能正常绑定:
- 编写附加属性类:
public static class DataGridExtensions { public static readonly DependencyProperty SelectedCountProperty = DependencyProperty.RegisterAttached( "SelectedCount", typeof(int), typeof(DataGridExtensions), new PropertyMetadata(0)); public static int GetSelectedCount(DependencyObject obj) => (int)obj.GetValue(SelectedCountProperty); public static void SetSelectedCount(DependencyObject obj, int value) => obj.SetValue(SelectedCountProperty, value); public static readonly DependencyProperty TrackSelectedCountProperty = DependencyProperty.RegisterAttached( "TrackSelectedCount", typeof(bool), typeof(DataGridExtensions), new PropertyMetadata(false, OnTrackSelectedCountChanged)); public static bool GetTrackSelectedCount(DependencyObject obj) => (bool)obj.GetValue(TrackSelectedCountProperty); public static void SetTrackSelectedCount(DependencyObject obj, bool value) => obj.SetValue(TrackSelectedCountProperty, value); private static void OnTrackSelectedCountChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is DataGrid dataGrid) { if ((bool)e.NewValue) { dataGrid.SelectionChanged += DataGrid_SelectionChanged; UpdateSelectedCount(dataGrid); } else { dataGrid.SelectionChanged -= DataGrid_SelectionChanged; } } } private static void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (sender is DataGrid dataGrid) UpdateSelectedCount(dataGrid); } private static void UpdateSelectedCount(DataGrid dataGrid) { SetSelectedCount(dataGrid, dataGrid.SelectedItems.Count); } }
- XAML中使用:
<!-- 先引入命名空间,local是你的项目命名空间 --> <DataGrid x:Name="MyDataGrid" local:DataGridExtensions.TrackSelectedCount="True" /> <YourControl Visibility="{Binding ElementName=MyDataGrid, Path=(local:DataGridExtensions.SelectedCount), Converter={StaticResource CountToVisibilityConverter}}" />
方法三:结合ViewModel的MVVM方案
如果用MVVM模式,可以在ViewModel中维护选中计数,通过事件更新:
- ViewModel实现:
public class MainViewModel : INotifyPropertyChanged { private int _selectedItemsCount; public int SelectedItemsCount { get => _selectedItemsCount; set { if (_selectedItemsCount != value) { _selectedItemsCount = value; OnPropertyChanged(); } } } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } }
- 后台页面绑定ViewModel并监听事件:
public MainPage() { InitializeComponent(); ViewModel = new MainViewModel(); MyDataGrid.SelectionChanged += (s, e) => ViewModel.SelectedItemsCount = MyDataGrid.SelectedItems.Count; } public MainViewModel ViewModel { get; set; }
- XAML绑定:
<YourControl Visibility="{x:Bind ViewModel.SelectedItemsCount, Converter={StaticResource CountToVisibilityConverter}, Mode=OneWay}" />
内容的提问来源于stack exchange,提问作者miniHessel
相关产品推荐
相关产品推荐

