将 ItemContainerStyle 放在网格上

Putting an ItemContainerStyle on a Grid

显然 ItemContainerStyle 在 Grid 中不存在,这就是问题所在。

我的程序有一个显示一堆项目的 ListView。我想为每个项目添加一个按钮,以提供更多信息,而且我可以轻松完成。我遇到的问题是出现在每个项目上的边框,通过单击项目来切换 on/off 以显示当前选择的用户。我不希望这个边框包围我的按钮;我想把按钮放在带边框的位的右边。

我不能将按钮放在单独的列表上,否则我会得到两个不同的可滚动列表,因此它必须留在 ListView 中,但 ListView 上的任何边框都会包围 ListView 中的所有内容。我的解决方案是在 ListView 中制作一个带有两个网格的 StackPanel,其中第一个网格包含我所有的旧内容和可切换的边框,第二个网格只有我的按钮。所以现在我只需要将该边框移动到网格上...但是如何?

XAML代码:

<!-- Other code up above... -->
            <ListView x:Name="lstAvailableProjects"
                      Grid.Row="1" 
                      ItemsSource="{Binding ProjectList, Mode=TwoWay}"
                      Height="Auto"  
                      Width="Auto"
                      SelectionMode="Multiple"    
                      IsRightTapEnabled="True"
                      IsDoubleTapEnabled="False"
                      IsSwipeEnabled="False"
                      SelectionChanged="lstAvailableProjects_SelectionChanged" 
                      Background="Transparent"
                      VerticalAlignment="Top"
                      ItemContainerStyle="{StaticResource ActiveProjectListViewStyle}"
                      BorderThickness="30,0,0,0">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Grid HorizontalAlignment="Stretch" Width="250" Background="{Binding Converter={StaticResource projectStateColorConverter}}">
                                <Grid.RowDefinitions>
                                    <RowDefinition/>
                                    <RowDefinition/>
                                </Grid.RowDefinitions>
                                <TextBlock Text="{Binding ProjectName}" 
                                       Foreground="Black" 
                                       Margin="10 5 0 0"
                                       FontSize="17"
                                       VerticalAlignment="Center"
                                       Grid.Row="0"
                                       HorizontalAlignment="Stretch"/>
                                <TextBlock Text="{Binding AssignmentRole}"
                                       Visibility="{Binding ProjectAssignmentRole, Mode=OneWay, Converter={StaticResource visibilityConverter}}"
                                       Foreground="Black" 
                                       Margin="10 3 0 5"
                                       FontSize="14"
                                       Grid.Row="1"
                                       HorizontalAlignment="Stretch"/>
                                <TextBlock Text="{Binding StatusText}"
                                       Foreground="Red"
                                       Margin="10 3 0 5"
                                       Grid.Row="2"
                                       FontSize="14"/>
                            </Grid>
                            <Grid>
                                <Button>
                                    <Button.Template>
                                        <ControlTemplate>
                                            <Image Source="../Assets/setting_icon.png" Height="40" />
                                        </ControlTemplate>
                                    </Button.Template>
                                </Button>
                            </Grid>
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

由于网格项目被包装,除了覆盖 preparecontainerforitemoverride 之外,您不能在 xaml 中进行更改来实现它。

一个有点棘手的技巧是对按钮应用变换。

<Button.RenderTransform>
 <CompositeTransform TranslateX="100" TranslateY="100"/>
<Button.RenderTransform>