Wpf 如何根据项目的类别设置ListViewItem的样式
我记得,不久前,在MSDN上看到了一个示例,说明了如何根据项目中对象的类类型更改LitViewItem的样式 有人能告诉我这个例子的方向吗?我正在转换一个文件管理器,我希望使用这种方法 谢谢, 汤姆P 编辑: 好吧,我想我没有正确描述我的问题。让我试试代码:Wpf 如何根据项目的类别设置ListViewItem的样式,wpf,Wpf,我记得,不久前,在MSDN上看到了一个示例,说明了如何根据项目中对象的类类型更改LitViewItem的样式 有人能告诉我这个例子的方向吗?我正在转换一个文件管理器,我希望使用这种方法 谢谢, 汤姆P 编辑: 好吧,我想我没有正确描述我的问题。让我试试代码: public class IOItem { } public class FileItem : IOItem { } public class DirectoryItem : IOItem { } public class Netwo
public class IOItem
{
}
public class FileItem : IOItem
{
}
public class DirectoryItem : IOItem
{
}
public class NetworkItem : IOItem
{
}
现在,给定上述类,我可以创建一个基于最终对象的类类型更改的样式吗?例如:
<Style TargetType="{x:Type FileItem}">
<Setter Property="Background" Value="Red" />
</Style>
<Style TargetType="{x:Type DirectoryItem}">
<Setter Property="Background" Value="Green" />
</Style>
这可能吗?我认为您可以使用模板选择器 另一个选项是接口,接口中的一个属性将反映调用。
然后可以在XAML中使用模板 我认为您可以使用模板选择器 另一个选项是接口,接口中的一个属性将反映调用。
然后可以在XAML中使用模板 您可以将类类型的样式放在正在使用的列表控件的资源集合中,它们将覆盖您设置的任何全局样式
<ListView ItemsSource="{Binding Elements}">
<ListView.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBlock}">
<Rectangle Fill="Green" Width="100" Height="100"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Rectangle Fill="Red" Width="100" Height="100"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.Resources>
</ListView>
如果希望多个列表控件包含这些特定的类样式,请为列表控件创建一个样式,并在其资源中包含特定于类型的样式
<Window.Resources>
<Style x:Key="myListStyle" TargetType="{x:Type ListView}">
<Style.Resources>
<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Rectangle Fill="Red" Width="100" Height="100"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Style.Resources>
</Style>
</Window.Resources>
<ListView ItemsSource="{Binding Elements}" Style="{StaticResource myListStyle}" />
您可以将类类型的样式放在正在使用的列表控件的资源集合中,它们将覆盖您设置的任何全局样式
<ListView ItemsSource="{Binding Elements}">
<ListView.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBlock}">
<Rectangle Fill="Green" Width="100" Height="100"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Rectangle Fill="Red" Width="100" Height="100"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.Resources>
</ListView>
如果希望多个列表控件包含这些特定的类样式,请为列表控件创建一个样式,并在其资源中包含特定于类型的样式
<Window.Resources>
<Style x:Key="myListStyle" TargetType="{x:Type ListView}">
<Style.Resources>
<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Rectangle Fill="Red" Width="100" Height="100"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Style.Resources>
</Style>
</Window.Resources>
<ListView ItemsSource="{Binding Elements}" Style="{StaticResource myListStyle}" />
您需要创建一个
样式选择器
,并将其分配给ItemContainerStyleSelector
属性。在选择器中,只需根据项目的类型选择样式
class MyStyleSelector : StyleSelector
{
public override Style SelectStyle(object item, DependencyObject container)
{
if (item is FileItem)
return Application.Current.Resources["FileItemStyle"];
if (item is DirectoryItem)
return Application.Current.Resources["DirectoryItemStyle"];
return null;
}
}
您需要创建一个
样式选择器
,并将其分配给ItemContainerStyleSelector
属性。在选择器中,只需根据项目的类型选择样式
class MyStyleSelector : StyleSelector
{
public override Style SelectStyle(object item, DependencyObject container)
{
if (item is FileItem)
return Application.Current.Resources["FileItemStyle"];
if (item is DirectoryItem)
return Application.Current.Resources["DirectoryItemStyle"];
return null;
}
}
因为选择使用哪种样式的变量只不过是类型。。对于对象,您不需要任何形式的转换器或c#magic,只需在范围内为要更改的类型设置样式即可。(见我的答案,它很性感!)因为选择使用哪种样式的变量只不过是类型而已。。对于对象,您不需要任何形式的转换器或c#magic,只需在范围内为要更改的类型设置样式即可。(看我的答案,它很性感!)我发誓我看到了一个只有XAML的解决方案,但这是在XMAL第一次出现的时候。这是我想要的。谢谢你的帮助。我发誓我看到了一个只有XAML的解决方案,但这是在XMAL第一次出现的时候。这是我想要的。谢谢你的帮助。