【问题标题】:Set a padding on dataGridCells in WPF在 WPF 中的 dataGridCells 上设置填充
【发布时间】:2011-07-11 22:20:07
【问题描述】:

简单问题:如何在 WPF 中的 dataGridCell 上设置填充? (一次一个或在所有单元格上,我不在乎)

我尝试通过在DataGridCell.Padding 属性上添加一个setter 来使用DataGrid.CellStyle 属性,并以同样的方式使用DataGridColumn.CellStyle 属性,但没有任何效果。

我也尝试使用 DataGridColumn.ElementStyle 属性,但没有更多运气。

我有点卡在那里,有没有人设法在 dataGridCell 上应用填充?

注意:我要补充一点,不,我不能使用透明边框来执行此操作,因为我已经将边框属性用于其他用途。我也不能使用 margin 属性(这似乎有效,令人惊讶的是),因为我使用了 background 属性,并且我不希望我的单元格之间有任何“空白”空间。

【问题讨论】:

    标签: wpf datagrid styling


    【解决方案1】:

    问题是Padding 没有转移到DataGridCell 模板中的Border。您可以编辑模板并为Padding添加模板绑定

    <DataGrid ...>
        <DataGrid.CellStyle>
            <Style TargetType="DataGridCell">
                <Setter Property="Padding" Value="20"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type DataGridCell}">
                            <Border Padding="{TemplateBinding Padding}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                                <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </DataGrid.CellStyle>
        <!--...-->
    </DataGrid>
    

    【讨论】:

    • 好的,我接受了你的回答,尽管我找到了另一种方法(有点像黑客,但在我的情况下更容易):使用 ElementStyle 属性设置单元格内容的边距属性。不过你的解决方案更好。
    • @David:是的,这将是另一种方式。什么都能完成工作:)
    • 我要补充一点,就我而言,这是另一件让我认为 WPF 还远未完成的事情......
    • @FredrikHedblad 你怎么知道要在边框上应用 TemplateBinding 的哪些属性?
    • 我已向 WPF 团队提交了一个问题,以简化此方案:github.com/dotnet/wpf/issues/2874
    【解决方案2】:

    你也可以尝试改变

    {Binding BindingValue, StringFormat={}{0:#0.0000}}
    

    {Binding BindingValue, StringFormat={}{0:#0.0000 }}
    

    有趣的是,WPF 的 XAML {0:#0.0000 } 将以呈现控件的格式尊重这个额外的空格字符,以将您的值移出网格列的边缘。

    【讨论】:

      【解决方案3】:

      大约 5 年后,由于这个问题似乎仍然有用(它仍然得到了支持)并且由于它已被请求,这是我在 TextColumn 上使用的解决方案(使用 ElementStyle)(但你可以这样做任何类型的 DataGridColumn 都一样):

      我在后面的代码中完成了这一切:

      class MyTextColumn : DataGridTextColumn
      {
          public MyTextColumn()
          {
              ElementStyle = new Style(typeof(TextBlock));
              EditingElementStyle = new Style(typeof(TextBox));
      
              ElementStyle.Setters.Add(new Setter(FrameworkElement.MarginProperty, new Thickness(3)));
              EditingElementStyle.Setters.Add(new Setter(Control.PaddingProperty, new Thickness(0, 1, 0, 1)));
          }
      }
      

      但是如果你想直接在xaml中做:

      <DataGrid.Columns>
          <DataGridTextColumn>
              <DataGridTextColumn.ElementStyle>
                  <Style TargetType="TextBlock">
                      <Setter Property="Margin" Value="3"/>
                  </Style>
              </DataGridTextColumn.ElementStyle>
              <DataGridTextColumn.EditingElementStyle>
                  <Style TargetType="TextBox">
                      <Setter Property="Padding" Value="0 1 0 1"/>
                  </Style>
              </DataGridTextColumn.EditingElementStyle>
          </DataGridTextColumn>
      </DataGrid.Columns>
      

      【讨论】:

        【解决方案4】:

        这是一种结合 David 方法的更简洁的方法(我的观点)

        <Resources>
            <Style x:Key="ColumnElementStyle" TargetType="TextBlock">
                <Setter Property="Margin" Value="5,0,10,0" />
            </Style>
        </Resources>
        

        那么……

        <DataGridTextColumn ElementStyle="{StaticResource ColumnElementStyle}" />
        <DataGridTextColumn ElementStyle="{StaticResource ColumnElementStyle}" />
        

        (在我的情况下,我的行是只读的,所以没有 EditingStyle)

        【讨论】:

          【解决方案5】:
          <DataGrid.Columns>
                <DataGridTextColumn  MinWidth="100" Header="Changed by"  Width=""  Binding="{Binding Changedby}" IsReadOnly="True"  >
                  <DataGridTextColumn.CellStyle>
                    <Style TargetType="DataGridCell">
                    <Setter Property="BorderThickness" Value="0"/>
                    <Setter Property="Background" Value="Transparent" />
                   <Setter Property="FrameworkElement.HorizontalAlignment"Value="Center"/>
                    </Style>
                </DataGridTextColumn.CellStyle>
              </DataGridTextColumn>
          

          【讨论】:

            猜你喜欢
            • 2018-01-13
            • 1970-01-01
            • 1970-01-01
            • 2011-03-20
            • 2013-12-19
            • 2019-12-08
            • 1970-01-01
            • 1970-01-01
            • 2019-11-30
            相关资源
            最近更新 更多