自定义 ListViewItem 控件显示类名称,而常规 ListView 控件显示实际数据

问题描述 投票:0回答:2

以下是 WPF 应用程序的代码片段,其中我使用 ListView 控件和自定义 ListView 控件,但在 UI 中,自定义 ListView 控件显示类名称,而 List View 控件显示实际数据 [即Class1 的 Text1 属性值]。我知道通过重写 Class1 的 ToString() 我的自定义 ListView 显示实际数据,但我不想重写 ToString()。那么有没有办法更改我的自定义 ListView 的模板,使其工作方式类似于 ListView 控件的工作方式?

MainWindow.xaml:

<Window
                x:Class="Swip.MainWindow"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:controls="clr-namespace:Custom.Controls;assembly=Custom.Controls"
                xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                xmlns:local="clr-namespace:Swip"
                xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                Title="MainWindow"
                Width="525"
                Height="350"
                mc:Ignorable="d">

                <Grid d:DataContext="{d:DesignInstance d:Type=local:MainWindowModel}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="5*" />
                        <ColumnDefinition Width="5*" />
                    </Grid.ColumnDefinitions>

                    <controls:SwipableListView Grid.Column="0" ItemsSource="{Binding Items, Mode=OneWay}">
                        <controls:SwipableListView.View>
                            <GridView>
                                <GridView.Columns>
                                    <GridViewColumn DisplayMemberBinding="{Binding Path=Text1}" Header="11" />
                                </GridView.Columns>
                            </GridView>
                        </controls:SwipableListView.View>
                    </controls:SwipableListView>

                    <ListView Grid.Column="1" ItemsSource="{Binding Items, Mode=OneWay}">
                        <ListView.View>
                            <GridView>
                                <GridView.Columns>
                                    <GridViewColumn DisplayMemberBinding="{Binding Path=Text1}" Header="11" />
                                </GridView.Columns>
                            </GridView>
                        </ListView.View>
                    </ListView>             
                </Grid>             
            </Window>

MainWindow.xaml.cs

  /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext=new MainWindowModel();
        }
    }

视图模型

 public class MainWindowModel
                {
                    public MainWindowModel()
                    {
                        this.Items = new ObservableCollection<Class1>
                        {
                            new Class1(1),
                            new Class1(2),
                            new Class1(3),
                        };

                        this.DeleteCommand = new RoutedCommand("Delete", typeof(MainWindowModel));
                    }

                    public ObservableCollection<Class1> Items { get; private set; }

                    public ICommand DeleteCommand { get; set; }
                }

自定义控件模板

  <Style TargetType="{x:Type Custom_Control:SwipableListViewItem}">
    <Setter Property="SnapsToDevicePixels" Value="true"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="IsDeletePossible" Value="{Binding IsDeletePossible, RelativeSource={RelativeSource AncestorType={x:Type Custom_Control:SwipableListView}}}"/>
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    <Setter Property="OverridesDefaultStyle" Value="true"/>
    <Setter Property="Background" Value="{StaticResource Brush_ContainerFill}"/>
    <Setter Property="MinHeight" Value="48"/>
    <Setter Property="BorderBrush" Value="{StaticResource Brush_ContainerBorder}"/>
    <Setter Property="Foreground" Value="{StaticResource Brush_FontReadonly}"/>
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="BorderThickness" Value="1,0,1,1"/>
    <Setter Property="Padding" Value="16,2" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Custom_Control:SwipableListViewItem}">
                <Grid>
                    <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Border>
                    <Button Width="120" HorizontalAlignment="Right" Margin="0,5,5,5" x:Name="PART_DeleteButton" Content="{TemplateBinding ButtonText}" Visibility="Hidden"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="true">
                        <Setter Property="Background" TargetName="Bd" Value="{StaticResource Brush_ActionBackground}"/>
                        <Setter Property="Foreground" Value="White"/>
                    </Trigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsSelected" Value="true"/>
                            <Condition Property="Selector.IsSelectionActive" Value="false"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Background" TargetName="Bd" Value="{StaticResource Brush_ActionBackground}"/>
                        <Setter Property="Foreground" Value="White"/>
                    </MultiTrigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsSelected" Value="true"/>
                            <Condition Property="IsSelectedForDelete" Value="true"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Visibility" TargetName="PART_DeleteButton" Value="Visible"/>
                    </MultiTrigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="{StaticResource Brush_FontReadonly}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
c# wpf
2个回答
0
投票

按照 Clemens 的建议 - 通过将 ContentPresenter 更改为 GridViewRowPresenter 解决了该问题。 请参阅:ListView 样式和模板


0
投票

在 iOS 上的 .net MAUI 应用程序中,我通过将 DataTemplate 内的所有内容包装在 ViewCell 内来解决此问题。我的 ListView 看起来像这样:

<ListView x:Name="SearchListView" SeparatorColor="Black" RowHeight="100" ItemsSource="{Binding ListViewSearchResult, Mode=OneWay}"
        >
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                <Grid ColumnDefinitions="75,*,120" Padding="3" Margin="10">
                    <Border
            WidthRequest="40"
            HeightRequest="40"
            StrokeShape="RoundRectangle 20 20"
            Stroke="Black"
            StrokeThickness="0.1"
            Grid.Column="0"
            VerticalOptions="Center"
            >
                        <Image Source="{Binding ProfilePic}" Aspect="AspectFill"/>
                    </Border>
                    <Label
            Grid.Column="1"
            HorizontalTextAlignment="Start"
            HorizontalOptions="StartAndExpand"
            VerticalTextAlignment="Center"
            Text="{Binding Name}" FontSize="Medium" TextColor="Black" VerticalOptions="Center"/>
                    <Button BackgroundColor="CornflowerBlue" Text="Add Friend" Grid.Column="2"
                VerticalOptions="Center" HorizontalOptions="Center" HeightRequest="35"
                Clicked="AddFriendButton_Clicked" WidthRequest="105" FontAttributes="Bold"/>
                </Grid>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
© www.soinside.com 2019 - 2024. All rights reserved.