如何使用全行选择和正确的主题获取XAML Listview

时间:2013-06-11 18:08:39

标签: xaml listview themes

我正在接管一些包含XAML Listview的代码。我注意到当前的Listview没有全行选择;也就是说,您无法单击行中的任何位置来选择项目,但必须单击文本占用的区域。仔细研究一下,我尝试将一个Horizo​​ntalContentAlignment =“stretch”添加到ItemContainerStyle,但现在这些项目不再使用与对话框的其余部分(ExpressionDark)相同的主题。

原始XAML代码:

Title="SelectUser" Height="350" Width="480" WindowStartupLocation="CenterScreen" WindowStyle="None" SizeToContent="Height" ResizeMode="NoResize">
<expressionDark:ExpressionDarkTheme>
    <Border BorderBrush="DimGray" BorderThickness="2">
        <StackPanel Margin="12,12,12,12">
            <TextBlock FontSize="16" FontWeight="Bold" HorizontalAlignment="Center" Margin="0,0,0,12">Select User</TextBlock>
                <ListView Name="listViewUsers" Height="200"  Width="400" ItemsSource="{Binding}" SelectionChanged="listViewUsers_SelectionChanged">
                <ListView.View>
            <GridView>
                <GridViewColumn Header="User Id" DisplayMemberBinding="{Binding Path=UserID}" Width="150"/>
                <GridViewColumn Header="User Name" DisplayMemberBinding="{Binding Path=UserName}" Width="250"/>
            </GridView>
        </ListView.View>
    </ListView>
            <UniformGrid Height="23" Rows="1" Columns="2" Margin="0,16,0,12" Width="Auto">
                <Button Name="buttonCancel" HorizontalAlignment="Center" Width="75" Height="23" IsCancel="True" Click="buttonCancel_Click">Cancel</Button>
                <Button Name="buttonOK" HorizontalAlignment="Center" Width="75" Height="23" IsDefault="True" Click="buttonOK_Click">OK</Button>
            </UniformGrid>
        </StackPanel>
    </Border>
</expressionDark:ExpressionDarkTheme>

Correct appearance, but no full-row select

但是当我像这样添加ItemContainerStyle时:

Title="SelectUser" Height="350" Width="480" WindowStartupLocation="CenterScreen" WindowStyle="None" SizeToContent="Height" ResizeMode="NoResize">
<expressionDark:ExpressionDarkTheme>
    <Border BorderBrush="DimGray" BorderThickness="2">
        <StackPanel Margin="12,12,12,12">
            <TextBlock FontSize="16" FontWeight="Bold" HorizontalAlignment="Center" Margin="0,0,0,12">Select User</TextBlock>
                <ListView Name="listViewUsers" Height="200"  Width="400" ItemsSource="{Binding}" SelectionChanged="listViewUsers_SelectionChanged">
                <ListView.ItemContainerStyle>
                    <Style TargetType="ListViewItem">
                        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                    </Style>
                </ListView.ItemContainerStyle>
                <ListView.View>
            <GridView>
                <GridViewColumn Header="User Id" DisplayMemberBinding="{Binding Path=UserID}" Width="150"/>
                <GridViewColumn Header="User Name" DisplayMemberBinding="{Binding Path=UserName}" Width="250"/>
            </GridView>
        </ListView.View>
    </ListView>
            <UniformGrid Height="23" Rows="1" Columns="2" Margin="0,16,0,12" Width="Auto">
                <Button Name="buttonCancel" HorizontalAlignment="Center" Width="75" Height="23" IsCancel="True" Click="buttonCancel_Click">Cancel</Button>
                <Button Name="buttonOK" HorizontalAlignment="Center" Width="75" Height="23" IsDefault="True" Click="buttonOK_Click">OK</Button>
            </UniformGrid>
        </StackPanel>
    </Border>
</expressionDark:ExpressionDarkTheme>

Full-row select works, but Listview items have incorrect colors

我是XAML的新手,所以我不确定我在这里做错了什么。我是否需要将ExpressionDark主题应用于Listview项目?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。我的所有控件都使用ExpressionDark主题。

这就是我解决这个问题的方法:

<ListBox HorizontalContentAlignment="Stretch">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid Background="Transparent">
                <!-- here is my custom content -->
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

在Grid元素中添加Background="Transparent"属性后,一切正常。