[英]WPF. How to add different styles on ListViewItems on code behind?
How can I apply different styles from source file to each ListViewItem in ListView, depends on thier object type (System.IO.DirectoryInfo and System.IO.FileInfo)?如何将源文件中的不同 styles 应用到 ListView 中的每个 ListViewItem,取决于它们的 object 类型(System.IO.DirectoryInfo 和 System.IO.FileInfo)?
There's simple XAML code that create ListView instance:有创建 ListView 实例的简单 XAML 代码:
<Window x:Class="WPF_Research.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:WPF_Research"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<StackPanel>
<ListView Name="lvRoot"/>
</StackPanel>
</Grid>
</Window>
And there's code behind where the list fills:列表填充的后面有代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
RefreshFolder();
}
public void RefreshFolder()
{
DirectoryInfo rootFolder = new DirectoryInfo(@"C:\");
var rootDirs = rootFolder.GetDirectories();
var rootFiles = rootFolder.GetFiles();
foreach(var dir in rootDirs)
{
lvRoot.Items.Add(dir.Name);
}
foreach (var file in rootFiles)
{
lvRoot.Items.Add(file.Name);
}
}
}
And resource file:和资源文件:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="lviDirectories" TargetType="ListViewItem">
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="BorderThickness" Value="3"/>
<Setter Property="FontSize" Value="18"/>
</Style>
<Style x:Key="lviFiles" TargetType="ListViewItem">
<Setter Property="BorderBrush" Value="Red"/>
<Setter Property="BorderThickness" Value="3"/>
<Setter Property="FontSize" Value="16"/>
</Style>
</ResourceDictionary>
I'm new to WPF.我是 WPF 的新用户。
You can do this as follows:您可以按如下方式执行此操作:
<Window.Resources>
<Style x:Key="lviDirectories" TargetType="ListViewItem">
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="BorderThickness" Value="3"/>
<Setter Property="FontSize" Value="18"/>
</Style>
<Style x:Key="lviFiles" TargetType="ListViewItem">
<Setter Property="BorderBrush" Value="Red"/>
<Setter Property="BorderThickness" Value="3"/>
<Setter Property="FontSize" Value="16"/>
</Style>
</Window.Resources>
<Grid>
<StackPanel VerticalAlignment="Center" Width="100">
<ListView ItemsSource="{Binding MyCollection}">
<ListView.ItemContainerStyleSelector>
<local:MyStyleSelector Style1="{StaticResource lviDirectories}" Style2="{StaticResource lviFiles}"/>
</ListView.ItemContainerStyleSelector>
</ListView>
</StackPanel>
</Grid>
public class MainWindowViewModel
{
public ObservableCollection<string> MyCollection { get; set; } = new()
{
"a", "b", "c", "d", "e", "f", "g",
};
}
public class MyStyleSelector : StyleSelector
{
public Style Style1 { get; set; }
public Style Style2 { get; set; }
public override Style SelectStyle(object item, DependencyObject container)
{
var s = (string)item;
return s is "a" or "c" or "e" or "g" ? Style1 : Style2;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.