如何获取数据模板以填充统一网格单元?

时间:2014-07-18 12:24:36

标签: wpf xaml uniformgrid

这是我的xaml。我需要红色网格来填充整个单元格。

<UserControl x:Class="Atelis.Tcc.Client.CheckList_TileView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300"
         DataContext="{Binding Model}">
<ListBox ItemsSource="{Binding Checks}" SelectedItem="{Binding SelectedCheck}">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Columns=" 3 "/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid Background="Red" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                <Grid.InputBindings>
                    <MouseBinding Gesture="LeftDoubleClick" Command="{Binding Path=DataContext.OpenSelectedCheck, ElementName=TileView}"/>
                </Grid.InputBindings>
                <StackPanel>
                    <TextBlock Text="{Binding CheckRoot.CheckNum}"/>
                    <TextBlock Text="{Binding CheckRoot.CheckDate}"/>
                </StackPanel>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

我的目标是允许用户单击单元格中的任意位置以执行InputBinding命令。因此,如果有另一种方法可以实现这一目标,那么这也足以作为答案。

enter image description here

1 个答案:

答案 0 :(得分:3)

如果我纠正你,那么你的单元格似乎没有在列表框中完全展开

所以设置HorizontalContentAlignment="Stretch"&amp; VerticalContentAlignment="Stretch"会做到这一点

<ListBox HorizontalContentAlignment="Stretch" 
         VerticalContentAlignment="Stretch"
         ItemsSource="{Binding Checks}" 
         SelectedItem="{Binding SelectedCheck}" >

替代方法

<ListBox ItemsSource="{Binding Checks}" 
         SelectedItem="{Binding SelectedCheck}" >
    <ListBox.Resources>
        <Style TargetType="ListBoxItem">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <ContentPresenter />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListBox.Resources>

此方法还可以帮助您删除所选背景(通常为蓝色),如果您也想删除它