programing

FullRow 선택 모드에서 DataGrid 현재 셀 테두리 사용 안 함

linuxpc 2023. 4. 16. 14:41
반응형

FullRow 선택 모드에서 DataGrid 현재 셀 테두리 사용 안 함

행 선택 모드에서 DataGrid를 사용하고 있습니다(즉,SelectionUnit="FullRow"사용자가 행을 강조 표시할 때 현재 셀 주위에 배치되어 있는 테두리를 삭제하기만 하면 진정한 전체 행을 선택할 수 있습니다(셀 수준 선택 없음).현재의 셀을 유지하는 그리드의 개념에 개의치 않고, 현재의 셀의 스타일을 변경함으로써, 귀찮은 현재의 셀 테두리를 제거하고 싶을 뿐입니다.가장 쉬운 방법은 무엇일까요?

를 설정할 수 있습니다.BorderThickness위해서DataGridCell0까지

<DataGrid ...
          SelectionUnit="FullRow">
    <DataGrid.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="BorderThickness" Value="0"/>
            <!-- Update from comments.
                 Remove the focus indication for the selected cell -->
            <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        </Style>
    </DataGrid.CellStyle>
    <!-- ... -->
</DataGrid>

여기서 가까운 다른 답을 보았지만 포커스 직사각형을 제거하지는 못했습니다.여기 모든 경계를 없애는 방법이 있습니다.

<DataGrid.Resources>
    <Style TargetType="{x:Type DataGridCell}">
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
    </Style>
</DataGrid.Resources>

또한 기술적으로 이러한 셀은 아직 포커스가 있기 때문에(보이지 않을 뿐), 탭 키를 다음 셀이 아닌 다음 행으로 이동시키기 위해 위에 따라 셀 스타일을 정의하지만 다음 항목도 추가합니다.

<DataGrid.Resources>
    <Style x:Key="NoFocusDataGridCell" TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource {x:Type DataGridCell}}">
        <Setter Property="Focusable"        Value="False" />
        <Setter Property="IsTabStop"        Value="False" />
        <Setter Property="IsHitTestVisible" Value="False" />
    </Style>
</DataGrid.Resources>

...첫 번째 열 정의를 제외한 모든 열에 적용합니다.그러면 탭 키가 다음 셀이 아닌 다음 행으로 이동합니다.

하지만 다시 국경으로 돌아갑시다.공백 이유를 위해 레이아웃에 포함시키려면 위의 항목을 이 항목으로 변경하십시오.

<DataGrid.Resources>
    <Style TargetType="{x:Type DataGridCell}">
        <Setter Property="BorderBrush" Value="Transparent" />
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
    </Style>
</DataGrid.Resources>

즐기세요! :)

<Style x:Key="DataGrid" TargetType="DataGrid">
    <Setter Property="CellStyle">
        <Setter.Value>
            <Style TargetType="DataGridCell">
                <Setter Property="BorderThickness" Value="0"/>
                <Setter Property="Foreground" Value="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}}" />
                <Setter Property="Background" Value="{Binding Background, RelativeSource={RelativeSource TemplatedParent}}" />
            </Style>
        </Setter.Value>
    </Setter>
</Style>

정답은 다음과 같습니다.

<!-- put it in your cell style -->
<DataTrigger Binding="{Binding SelectionUnit, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Value="FullRow">
    <Setter Property="BorderThickness" Value="0" />
</DataTrigger>

질문은 FullRow 선택 모드에서만 비활성화하는 것이었지만 다른 답변에서는 셀 선택 모드에서도 완전히 비활성화하는 솔루션을 제공합니다.

매우 오래된 질문에 대한 답변이지만 믿거나 말거나 WPF와 WinForms는 오늘날에도 여전히 사용되고 있습니다.

만약 당신이 XAML 스타일을 망치고 싶지 않다면, 당신은 이 간단한 해킹을 할 수 있습니다.XAML 스타일만큼 잘 동작하지는 않지만, 자신에게 맞는지 시험해 볼 수 있습니다.단순히 셀을 클릭하는 것은 괜찮지만, 셀을 드래그하려고 해도 나중에 포커스가 지워지지 않습니다(다른 케이스를 추가할 수 있다고 확신하지만).

private void YourDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    YourDataGrid.Focus();
}

PS: 이벤트 핸들러를 추가하는 것을 잊지 마세요.DataGridSelectionChanged소유물.

셀이 편집 가능하고 선택되었을 때만 테두리를 표시하려면 DataGridCell 템플릿을 재정의하고 IsReadOnly가 아닌 셀이 선택되었을 때의 멀티트리거를 추가할 수 있습니다.열 또는 DataGrid에 대해 IsReadOnly = true를 설정하면 셀에 대한 테두리가 표시되지 않습니다.

<ControlTemplate x:Key="MellowDataGridCellTemplate" TargetType="{x:Type DataGridCell}">
    <Grid>
        <ContentPresenter VerticalAlignment="Center" />
        <Rectangle Name="FocusVisual" Stroke="White" StrokeThickness="1" Fill="Transparent" HorizontalAlignment="Stretch" 
                           VerticalAlignment="Stretch" IsHitTestVisible="false" Opacity="0" />

    </Grid>
    <ControlTemplate.Triggers>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsReadOnly" Value="False" />
                <Condition Property="IsSelected" Value="True" />
            </MultiTrigger.Conditions>
            <Setter TargetName="FocusVisual" Property="Opacity" Value="1"/>
        </MultiTrigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

스타일에서 템플릿 사용

<Style TargetType="{x:Type DataGridCell}" x:Key="MellowGridDataGridCell">
    <Setter Property="Template" Value="{StaticResource MellowDataGridCellTemplate}" />
</Style>

그리고 스타일을 사용하세요.

<DataGrid CellStyle={StaticResource MellowGridDataGridCell >
    ...
</DataGrid>

'아예'를한다면,xceed DataGridControl 해서 해 주세요.NavigationBehavior로로 합니다.RowOnly

<xcdg:DataGridControl NavigationBehavior="RowOnly" SelectionMode="Single"  ....

언급URL : https://stackoverflow.com/questions/4547370/disable-datagrid-current-cell-border-in-fullrow-selection-mode

반응형