【问题标题】:Make Silverlight DataGridCell contents fill the cell?使 Silverlight DataGridCell 内容填充单元格?
【发布时间】:2015-02-06 00:47:02
【问题描述】:

我正在尝试识别 DataGrid 中每个单元格上的 MouseLeftButtonDown,但是有两件事阻止了我:

  • DataGridCell 不会触发点击事件
  • 使用网格(带有单击事件)作为子元素不会填充单元格,因此只能在其内容上单击,它会自动调整大小以适应。

这是我的单元格模板:

    <DataTemplate x:Key="...">
    <Grid MouseLeftButtonDown="..."
          VerticalAlignment="Stretch"
          HorizontalAlignment="Stretch">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Image     HorizontalAlignment="Center"
                   VerticalAlignment="Center"
                   Width="24" Height="24"
                   Source="..."/>
    </Grid>
    </DataTemplate>

行高/列宽意味着单元格的高度为 64 像素,宽度为 80 像素,并且可以调整大小,因此内部的 24x24 图像是可能较大的单元格中唯一可点击的部分。 DataGridCell 本身的样式如下:

    <Style TargetType="data:DataGridCell" x:Key="...">
      <Setter Property="Height" Value="64"/>
      <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
      <Setter Property="VerticalContentAlignment" Value="Stretch"/>
    </Style>

理想情况下,网格应该扩展到包含单元格的大小。但是无论我将对齐属​​性分配给拉伸多少次,它都不会。您可以通过设置 Grid 的背景来查看。它符合其内容,仅此而已。

我已经设法通过在第 0 列和第 2 列中放置透明 Canvas 元素并设置列 maxwidth 来破解一个解决方案,但我觉得必须有更好的方法。

【问题讨论】:

    标签: wpf silverlight datagrid silverlight-4.0


    【解决方案1】:

    实际上,Grid 已经填满了单元格。您可以为 Grid 设置背景,如下所示:

      <Grid MouseLeftButtonDown="Grid_MouseLeftButtonDown"  Background="Red"
          VerticalAlignment="Stretch"
          HorizontalAlignment="Stretch" >
    

    然后尝试点击它,就会触发 MouseLeftButtonDown 事件。

    原因是,Grid 的 Background 默认为 null,表示 UI 上没有绘图区域,所以不会触发任何事件。您可以改为设置Background="Transparent"

    Background="Transparent"Background= null不同,Background="Transparent"表示控件有背景,有绘图区。

    这是我的例子:

    <Window.Resources>
        <Style TargetType="DataGridCell">
            <Setter Property="Height" Value="64"/>
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            <Setter Property="VerticalContentAlignment" Value="Stretch"/>
        </Style>
    </Window.Resources>
    
      <DataGrid x:Name="dg">
        <DataGrid.Columns>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Grid MouseLeftButtonDown="Grid_MouseLeftButtonDown" Background="Red"
          VerticalAlignment="Stretch"
          HorizontalAlignment="Stretch">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Image     HorizontalAlignment="Center"
                   VerticalAlignment="Center"
                   Width="24" Height="24"
                   Source="add.png"/>
                        </Grid>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
    
    
    public MainWindow()
    {
        InitializeComponent();
        List<string> lst = new List<string>() { "123" };
        dg.ItemsSource = lst;
    }
    
    private void Grid_MouseLeftButtonDown(object sender,      MouseButtonEventArgs e)
    {
        MessageBox.Show("hit");
    }
    

    【讨论】:

    • @cormacrelf 如果您为网格设置背景,您会看到网格填充单元格吗?
    • @cormacrelf 我认为它的风格有问题。看我的例子。
    • 在该图像链接中,网格不会填充单元格。这太疯狂了。我可能会坚持我的技巧。
    • @cormacrelf 我猜是风格问题。我不知道你的整个代码,你可以看看我的例子并与你的项目进行比较。
    • 感谢您的帮助@Rang
    猜你喜欢
    • 1970-01-01
    • 2014-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多