2

我正在接管一些包含 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>

外观正确,但没有全行选择

但是当我像这样添加 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>

全行选择有效,但 Listview 项目的颜色不正确

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

4

1 回答 1

0

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

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

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

Background="Transparent"在 Grid 元素中添加属性后,这一切都开始正常工作。

于 2013-10-03T16:28:18.350 回答