根据项属性设置WPF ListViewItem格式

cyvaqqii  于 2023-05-30  发布在  其他
关注(0)|答案(1)|浏览(163)

虽然有许多帖子与ListBox有关,但ListView控件被证明是相当有抵抗力的。
简而言之,我想突出显示ListView中的某些行为“禁用”,但仍然允许用户搜索/查看它们的列表。
在注解掉的XAML部分,我可以为所有ListViewItem显式设置Background。然而,如果我尝试绑定它,它会失败……尽管它没有给予任何警告,运行得很好--它根本不起作用。
我可以显式地为每一列创建一个TextBlock,并在那里设置Background,但这将导致相当多的copypasta,我希望避免,因为我在现实世界的例子中有相当多的列。
我试过设置一个DataTemplate,但不知道如何允许每列发生绑定--我看到的所有示例都专门绑定到item属性(在我的示例中,是一列)。我需要这样一个模板来设置所有列的样式,但在其他地方指定列的内容。
我想我已经很接近了,但是我在某个地方漏掉了一些小的东西,或者有些东西没有正确连接。
对于berivity,复制的代码如下;

XAML

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="200" Width="200">
  <ListView Margin="5" VerticalAlignment="Stretch" 
              SelectionMode="Single" ItemsSource="{Binding Path=Items}"
              SelectedItem="{Binding SelectedItem,Mode=TwoWay}">
    <ListView.Resources>
      <Style TargetType="{x:Type ListViewItem}">
        <!--<Setter Property="Background" Value="{Binding Background}" />-->
        <!--<Setter Property="Background" Value="Blue" />-->
      </Style>
      <!--<DataTemplate DataType="{x:Type local:FooItemViewModel}">
        <TextBlock Background="{Binding Background}"></TextBlock>
      </DataTemplate>-->
    </ListView.Resources>
    <ListView.View>
      <GridView AllowsColumnReorder="True">
        <GridView.ColumnHeaderContainerStyle>
          <Style>
            <Setter Property="GridViewColumnHeader.HorizontalContentAlignment" Value="Left" />
            <Setter Property="GridViewColumnHeader.Padding" Value="10 0" />
          </Style>
        </GridView.ColumnHeaderContainerStyle>
        <GridViewColumn Header="ID" Width="Auto" DisplayMemberBinding="{Binding Id}" />
        <GridViewColumn Header="Barcode" Width="Auto" DisplayMemberBinding="{Binding Barcode}" />
        <GridViewColumn Header="Check" Width="Auto">
          <GridViewColumn.CellTemplate>
            <DataTemplate>
              <CheckBox IsChecked="{Binding Selected}"/>
            </DataTemplate>
          </GridViewColumn.CellTemplate>
        </GridViewColumn>
      </GridView>
    </ListView.View>
  </ListView>
</Window>

XAML的代码隐藏

using System.Collections.ObjectModel;
using System.Windows;

namespace WpfApp1
{
    public partial class MainWindow : Window
    {
        private FooViewModel _viewModel;

        public MainWindow()
        {
            InitializeComponent();
            _viewModel = new FooViewModel();
            DataContext = _viewModel;
            Loaded += MainWindow_Loaded;
        }

        public void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            _viewModel.Items = new ObservableCollection<FooItemViewModel>
            {
                new FooItemViewModel { Id = 1, Barcode = "test1"},
                new FooItemViewModel { Id = 2, Barcode = "test2", Selected = true},
                new FooItemViewModel { Id = 3, Barcode = "test3"},
            };
        }
    }
}

查看机型

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.CompilerServices;

namespace WpfApp1
{
    internal class BaseViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void UpdateProperty<T>(ref T toSet, T value, [CallerMemberName] string propertyName = null)
        {
            if ((toSet == null && value != null) || !toSet.Equals(value))
            {
                toSet = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

    internal class FooItemViewModel : BaseViewModel
    {
        private int _id;
        private string _barcode;
        private bool _selected;

        public int Id
        {
            get => _id; 
            set => UpdateProperty(ref _id, value);
        }

        public string Barcode
        {
            get => _barcode; 
            set => UpdateProperty(ref _barcode, value);
        }

        public bool Selected
        {
            get => _selected; 
            set => UpdateProperty(ref _selected, value);
        }

        public Color Background => Color.Blue;
    }

    internal class FooViewModel : BaseViewModel
    {
        private FooItemViewModel _selectedItem;
        private ObservableCollection<FooItemViewModel> _items = new ObservableCollection<FooItemViewModel>();

        public FooItemViewModel Selecteditem
        {
            get => (FooItemViewModel)_selectedItem; 
            set => UpdateProperty(ref _selectedItem, value);
        }

        public ObservableCollection<FooItemViewModel> Items
        {
            get => _items; 
            set => UpdateProperty(ref _items, value);
        }
    }
}
w8biq8rn

w8biq8rn1#

Background属性应该返回一个Brush而不是Color,以使绑定正常工作:

public Brush Background => Brushes.Blue;

相关问题