Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 基于类型的每个ListViewItem的不同颜色(WPF XAML)_C#_Wpf_Xaml_Binding_Styling - Fatal编程技术网

C# 基于类型的每个ListViewItem的不同颜色(WPF XAML)

C# 基于类型的每个ListViewItem的不同颜色(WPF XAML),c#,wpf,xaml,binding,styling,C#,Wpf,Xaml,Binding,Styling,我正在为一个学校项目而挣扎。我正在制作一个非常基本的应用程序,它可以读取选定文件夹的内容并在ListView中显示。我创建了一个名为IFiles的接口,它有三个子类:Image、Text和Folder。它们都实现了IFiles接口 我的问题主要是关于XAML中的设计。因为我希望每个listview项根据找到的对象类型拥有自己的颜色。因此,如果是文本文件,背景颜色应该是绿色,如果是图像,背景颜色应该是蓝色。我当前的解决方案部分工作,但它确实按照我的要求绘制整行 我确实可以为ListViewItem

我正在为一个学校项目而挣扎。我正在制作一个非常基本的应用程序,它可以读取选定文件夹的内容并在ListView中显示。我创建了一个名为IFiles的接口,它有三个子类:Image、Text和Folder。它们都实现了IFiles接口

我的问题主要是关于XAML中的设计。因为我希望每个listview项根据找到的对象类型拥有自己的颜色。因此,如果是文本文件,背景颜色应该是绿色,如果是图像,背景颜色应该是蓝色。我当前的解决方案部分工作,但它确实按照我的要求绘制整行

我确实可以为ListViewItem选择一种颜色,但我只知道如何一次完成所有操作,而不是一行

这是我的代码:

   <Window.Resources>
    <BitmapImage x:Key="ImgIcon" UriSource="/Images/photo.png" DecodePixelHeight="20" DecodePixelWidth="20"></BitmapImage>
    <BitmapImage x:Key="TextIcon" UriSource="/Images/textfile.png" DecodePixelHeight="20" DecodePixelWidth="20"></BitmapImage>

    <Style TargetType="{x:Type ListViewItem}">
        <Setter Property="Background" Value="Red"></Setter>
    </Style>

    <DataTemplate DataType="{x:Type model:Image}">
        <StackPanel Orientation="Horizontal" Background="Blue" >
            <Image Source="{DynamicResource ImgIcon}"></Image>
            <Label Content="Filename:"></Label>
            <Label Content="{Binding Name}"></Label>
            <Label Content="Size:"></Label>
            <Label Content="{Binding Size}"></Label>
        </StackPanel>
    </DataTemplate>
    <DataTemplate DataType="{x:Type model:Text}">
        <StackPanel Orientation="Horizontal" Background="Green" >
            <Image Source="{DynamicResource TextIcon}"></Image>
            <Label Content="Filename:"></Label>
            <Label Content="{Binding Name}"></Label>
            <Label Content="Size:"></Label>
            <Label Content="{Binding Size}"></Label>
        </StackPanel>
    </DataTemplate>
</Window.Resources>
<Grid>
    <Button Content="Load Folder" HorizontalAlignment="Left" Margin="315,355,0,0" VerticalAlignment="Top" Width="155" Height="40" Command="{Binding LoadFolderCMD}"/>
    <ListView ItemsSource="{Binding Files}" Margin="0,0,0,89" HorizontalAlignment="Left" Width="792"/>

</Grid>

这就是我执行它时的样子:

红色部分应与特定行中的内容具有相同的颜色


希望您能帮助我在ListView上设置HorizontalContentAlignment=“Stretch”


您还应该利用网格行和列将项目限制在单独的区域中。避免使用边距放置,边距和填充应用于定义元素之间的间隙,而不是它们的绝对位置

在ListView上设置HorizontalContentAlignment=“Stretch”


您还应该利用网格行和列将项目限制在单独的区域中。避免使用边距放置,边距和填充应用于定义元素之间的间隙,而不是它们的绝对位置

您可以通过将
ListViewItem
HorizontalContentAlignment
设置为
Stretch
并将一些填充移动到数据模板,使
ItemTemplate
填满可用空间:

<Style TargetType="{x:Type ListViewItem}">
    <Setter Property="Background" Value="Red"></Setter>
    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="Padding" Value="0" />
</Style>

<DataTemplate DataType="{x:Type model:Image}">
    <Border Background="Blue" Padding="4,1">
        <StackPanel Orientation="Horizontal">
            <Image Source="{DynamicResource ImgIcon}"></Image>
            <Label Content="Filename:"></Label>
            <Label Content="{Binding Name}"></Label>
            <Label Content="Size:"></Label>
            <Label Content="{Binding Size}"></Label>
        </StackPanel>
    </Border>
</DataTemplate>
<DataTemplate DataType="{x:Type model:Text}">
    <Border Background="Green" Padding="4,1">
        <StackPanel Orientation="Horizontal">
            <Image Source="{DynamicResource TextIcon}"></Image>
            <Label Content="Filename:"></Label>
            <Label Content="{Binding Name}"></Label>
            <Label Content="Size:"></Label>
            <Label Content="{Binding Size}"></Label>
        </StackPanel>
    </Border>
</DataTemplate>

您可以通过将
ListViewItem
HorizontalContentAlignment
设置为
Stretch
并将一些填充移动到数据模板,使
ItemTemplate
填满可用空间:

<Style TargetType="{x:Type ListViewItem}">
    <Setter Property="Background" Value="Red"></Setter>
    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="Padding" Value="0" />
</Style>

<DataTemplate DataType="{x:Type model:Image}">
    <Border Background="Blue" Padding="4,1">
        <StackPanel Orientation="Horizontal">
            <Image Source="{DynamicResource ImgIcon}"></Image>
            <Label Content="Filename:"></Label>
            <Label Content="{Binding Name}"></Label>
            <Label Content="Size:"></Label>
            <Label Content="{Binding Size}"></Label>
        </StackPanel>
    </Border>
</DataTemplate>
<DataTemplate DataType="{x:Type model:Text}">
    <Border Background="Green" Padding="4,1">
        <StackPanel Orientation="Horizontal">
            <Image Source="{DynamicResource TextIcon}"></Image>
            <Label Content="Filename:"></Label>
            <Label Content="{Binding Name}"></Label>
            <Label Content="Size:"></Label>
            <Label Content="{Binding Size}"></Label>
        </StackPanel>
    </Border>
</DataTemplate>


您的数据模板几乎相同。我建议您仅使用一个数据模板,并根据项目类型选择颜色,请参见例如。然后,您可以为整个
ListViewItem
设置样式-它将使用基于项目类型的颜色,而不是硬编码的红色。您的数据模板几乎相同。我建议您仅使用一个数据模板,并根据项目类型选择颜色,请参见例如。然后,您可以为整个
ListViewItem
设置样式-它将使用基于项目类型的颜色,而不是硬编码的红色。@Lukas Méndez Duus:您尝试过吗?@Lukas Méndez Duus:您尝试过吗?