programing

WPF의 dataGridCells에 패딩 설정

golfzon 2023. 4. 9. 22:36
반응형

WPF의 dataGridCells에 패딩 설정

간단한 질문:WPF의 dataGridCell에 패딩을 설정하는 방법(한 번에 하나씩 또는 모든 셀에 대해 상관 없음)

를 사용해 본 적이 있습니다.DataGrid.CellStylesetter를 추가하여 속성을 설정합니다.DataGridCell.Padding사용뿐만 아니라DataGridColumn.CellStyle아무런 효과도 없이 동일한 방식으로 재산을 소유합니다.

저도 한번 써봤어요.DataGridColumn.ElementStyle더 이상 운이 없는 재산입니다.

제가 좀 곤란한데, 누가 패딩을 dataGridCell에 적용받았나요?

NB: 아니요, 투명 테두리를 사용할 수 없습니다. 이미 테두리 속성을 다른 용도로 사용하고 있기 때문입니다.또한 배경 속성을 사용하고 셀 사이에 공백 공간을 두지 않기 때문에 마진 속성을 사용할 수 없습니다(놀랍게도 충분히 작동하는 것 같습니다).

문제는 이 시스템이Padding에 전송되지 않습니다.Border다음 템플릿에 있습니다.DataGridCell. 템플릿을 편집하여 TemplateBinding을 추가할 수 있습니다.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>

여기 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}" />

(내 경우 행은 읽기 전용이므로 Editing Style은 없습니다)

거의 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>

다른 방법을 시도해 볼 수도 있습니다.

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

로.

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

흥미롭게도 WPF의 XAML {0:#0.0000}은(는) 렌더링된 컨트롤 형식으로 이 여분의 공백 문자를 사용하여 값을 그리드 열의 가장자리에서 이동합니다.

<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>

언급URL : https://stackoverflow.com/questions/5246745/set-a-padding-on-datagridcells-in-wpf

반응형