Ниже приведен фрагмент кода приложения WPF, где я использую элемент управления ListView и мой пользовательский элемент управления ListView, но в пользовательском интерфейсе пользовательский элемент управления ListView показывает имя класса, где в качестве элемента управления List View отображаются фактические данные [т.е. значение свойства Text1Class1].Я знаю, переопределив ToString () Class1, мой пользовательский 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();
}
}
ViewModel
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>