WPF无法使用gridview更改listview背景

时间:2016-11-16 18:49:13

标签: c# wpf xaml gridview

我有这个标记,当IsEnabled = false时,我的背景颜色不会保持为null。我尝试了一个样式触发器,当它被禁用时会改变它但它没有工作。使用gridview禁用时,如何使ListView背景颜色为空?

 <ListView Background="{x:Null}" IsEnabled="False">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName}"/>
                    <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LastName}"/>
                    <GridViewColumn Header="Email" DisplayMemberBinding="{Binding EmailAddress}"/>
                </GridView>
            </ListView.View>
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                </Style>
            </ListView.ItemContainerStyle>
        </ListView>

在app.xaml

<Style TargetType="ListView">
    <Setter Property="BorderThickness" Value="0"/>

    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Background" Value="Transparent" />
        </Trigger>
    </Style.Triggers>

</Style>

1 个答案:

答案 0 :(得分:1)

试试这个,

<ListView Background="Transparent" IsEnabled="False">
    <ListView.Template>
        <ControlTemplate TargetType="ListView">
            <Border Name="Border"                        
                    BorderThickness="1">
                <ScrollViewer Style="{DynamicResource {x:Static GridView.GridViewScrollViewerStyleKey}}">
                    <ItemsPresenter />
                </ScrollViewer>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsGrouping" Value="true">
                    <Setter Property="ScrollViewer.CanContentScroll" Value="false" />
                </Trigger>
                <Trigger Property="IsEnabled" Value="false">
                    <Setter TargetName="Border" Property="Background" Value="Transparent" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>

    </ListView.Template>
    <ListView.View>
        <GridView>
            <GridViewColumn DisplayMemberBinding="{Binding FirstName}" Header="First Name" />
            <GridViewColumn DisplayMemberBinding="{Binding LastName}" Header="Last Name" />
            <GridViewColumn DisplayMemberBinding="{Binding EmailAddress}" Header="Email" />
        </GridView>
    </ListView.View>
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    </ListView.ItemContainerStyle>
</ListView>