WPF 목록 보기 해제 선택
WPF의 할 수 ?ListView
사용자가 행을 클릭하면 행이 강조 표시되지 않습니까?
(출처: konim5am, artax.karlin.mff.cuni.cz )
저는 1행을 클릭했을 때 0행과 같은 모양으로 하고 싶습니다.
관련이 있을 수 있음: 호버/선택 항목의 모양을 스타일링할 수 있습니까?예를 들어 파란색 그라데이션 호버 룩(라인 3)을 사용자 지정 단색으로 대체합니다.나는 이것과 이것을 찾았지만, 안타깝게도 도움이 되지 않습니다.
ListView를 사용하지 않고도 동일한 작업을 수행할 수 있습니다.ListView처럼 논리 스크롤과 UI 가상화를 사용할 수 있으면 좋겠습니다.)
ListView용 XAML은 다음과 같습니다.
<ListView Height="280" Name="listView">
<ListView.Resources>
<!-- attempt to override selection color -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightColorKey}"
Color="Green" />
</ListView.Resources>
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
<!-- more columns -->
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
Martin Konicek의 설명에 따라 가장 간단한 방법으로 항목 선택을 완전히 비활성화합니다.
<ListView>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Focusable" Value="false"/>
</Style>
</ListView.ItemContainerStyle>
...
</ListView>
그러나 항목을 선택할 수 있는 것과 같은 ListView 기능이 여전히 필요한 경우 다음과 같이 선택한 항목의 스타일을 시각적으로 비활성화할 수 있습니다.
목록 보기를 변경하는 방법에서 여러 가지 방법으로 이 작업을 수행할 수 있습니다.항목의 제어 템플릿을 스타일만 설정할 수 있습니다(훨씬 쉽습니다).ListView에 대한 스타일을 작성할 수 있습니다.ItemContainerStyle을 사용하는 항목을 선택할 때 배경 및 테두리 브러시를 '해제'합니다.
<ListView>
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Style.Triggers>
<Trigger Property="IsSelected"
Value="True">
<Setter Property="Background"
Value="{x:Null}" />
<Setter Property="BorderBrush"
Value="{x:Null}" />
</Trigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
...
</ListView>
또한 항목이 선택되었을 때(또는 테스트용으로만) 사용자에게 알릴 다른 방법이 없는 한 값을 나타내는 열을 추가할 수 있습니다.
<GridViewColumn Header="IsSelected"
DisplayMemberBinding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListViewItem}}, Path=IsSelected}" />
무어의 답변은 효과가 없습니다. 여기 페이지가 있습니다.
목록 상자의 항목에 대한 선택 색상, 내용 정렬 및 배경색 지정
작동하지 않는 이유를 설명합니다.
목록 보기에 기본 텍스트만 포함되어 있는 경우 문제를 해결하는 가장 간단한 방법은 투명 브러시를 사용하는 것입니다.
<Window.Resources>
<Style TargetType="{x:Type ListViewItem}">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#00000000"/>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#00000000"/>
</Style.Resources>
</Style>
</Window.Resources>
목록 보기의 셀이 콤보 상자와 같은 컨트롤을 보유하고 있으면 색상도 변경되므로 바람직하지 않은 결과가 발생합니다.이 문제를 해결하려면 컨트롤의 템플릿을 다시 정의해야 합니다.
<Window.Resources>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Border SnapsToDevicePixels="True"
x:Name="Bd"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="{TemplateBinding Padding}">
<GridViewRowPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Columns="{TemplateBinding GridView.ColumnCollection}"
Content="{TemplateBinding Content}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled"
Value="False">
<Setter Property="Foreground"
Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
각 목록 보기의 스타일 설정Focusable을 false로 설정할 항목입니다.
<ListView ItemsSource="{Binding Test}" >
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="Focusable" Value="False"/>
</Style>
</ListView.ItemContainerStyle>
</ListView>
ListView의 기본 템플릿입니다.혼합된 항목:
기본 목록 보기항목 템플릿:
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="true"/>
<Condition Property="Selector.IsSelectionActive" Value="false"/>
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}}"/>
</MultiTrigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
기본 템플릿을 대체할 아래 코드를 스타일에 추가하여 IsSelected Trigger 및 IsSelected/IsSelected Active MultiTrigger를 제거하기만 하면 선택 시 시각적 변화는 없습니다.
IsSelected 속성의 시각적 변경을 해제하는 솔루션:
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
내가 찾은 가장 쉬운 방법은:
<Setter Property="Focusable" Value="false"/>
좋아요, 게임에 조금 늦었지만, 이 해결책들 중 어느 것도 제가 하려고 했던 것을 제대로 해내지 못했습니다.이러한 솔루션에는 몇 가지 문제가 있습니다.
- 목록 보기 사용 안 함스타일을 고정하고 모든 자식 컨트롤을 비활성화하는 항목
- 히트 테스트 스택에서 제거합니다. 즉, 어린이 컨트롤은 마우스를 넘거나 클릭하지 않습니다.
- 초점을 맞추지 못하게 해요, 이건 그냥 나한테 안 먹힌 거예요?
그룹화 헤더와 각 ListView가 있는 ListView를 원했습니다.항목은 선택하거나 위로 마우스를 이동하지 않고 '정보'로만 표시되어야 하지만 목록 보기항목에 클릭할 수 있고 마우스 커서를 이동할 수 있는 단추가 있습니다.
그래서 제가 정말 원하는 것은 ListView입니다.목록 보기가 아닌 항목아이템이 아예, 그래서, 저는 리스트뷰를 오버라이드했습니다.항목의 제어 템플릿을 사용하여 단순한 내용 제어를 만들었습니다.
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<ContentControl Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
위의 해결책으로 더 나아가면...MultiTrigger를 사용하여 MouseOver 하이라이트를 선택한 후에도 계속 작동하여 ListView를 사용할 수 있습니다.항목의 스타일:
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="True" />
<Condition Property="IsMouseOver" Value="False" />
</MultiTrigger.Conditions>
<MultiTrigger.Setters>
<Setter Property="Background" Value="{x:Null}" />
<Setter Property="BorderBrush" Value="{x:Null}" />
</MultiTrigger.Setters>
</MultiTrigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
중 하나는 보속중성다같습다니음과목하는나록기의입니다.IsHitTestVisible
선택을 취소합니다.
아래 코드를 사용합니다.
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="Background" Value="Transparent" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<ContentPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
이는 다음과 같은 요구 사항을 충족할 수 있는 다른 사용자를 위한 것입니다.
- 표준 강조 표시의 색상만 변경하는 것이 아니라 "선택됨"의 시각적 표시(예: 어떤 모양 사용)를 완전히 대체합니다.
- 선택한 이 표시를 모델의 다른 시각적 표현과 함께 데이터 템플릿에 포함합니다.
- 모델 클래스에 "Is SelectedItem" 속성을 추가할 필요가 없으며 모든 모델 개체에서 해당 속성을 수동으로 조작해야 합니다.
- 목록 보기에서 항목을 선택할 수 있어야 함
- 또한 IsMouseOver의 시각적 표현을 대체하고 싶습니다.
만약 당신이 나와 마찬가지로 (WPF를 .NET 4.5와 함께 사용) 스타일 트리거와 관련된 솔루션이 작동하지 않는다는 것을 알았다면, 다음과 같은 해결책이 있습니다.
목록 보기의 제어 템플릿 바꾸기스타일의 항목:
<ListView ItemsSource="{Binding MyStrings}" ItemTemplate="{StaticResource dtStrings}">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<ContentPresenter/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
</ListView>
..그리고 데이터 템플릿:
<DataTemplate x:Key="dtStrings">
<Border Background="LightCoral" Width="80" Height="24" Margin="1">
<Grid >
<Border Grid.ColumnSpan="2" Background="#88FF0000" Visibility="{Binding RelativeSource={RelativeSource AncestorType=ListViewItem}, Path=IsMouseOver, Converter={StaticResource conBoolToVisibilityTrueIsVisibleFalseIsCollapsed}}"/>
<Rectangle Grid.Column="0" Fill="Lime" Width="10" HorizontalAlignment="Left" Visibility="{Binding RelativeSource={RelativeSource AncestorType=ListViewItem}, Path=IsSelected, Converter={StaticResource conBoolToVisibilityTrueIsVisibleFalseIsCollapsed}}" />
<TextBlock Grid.Column="1" Text="{Binding}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White" />
</Grid>
</Border>
</DataTemplate>
런타임에 결과가 표시됩니다(항목 'B'가 선택되고 항목 'D'에 마우스가 표시됨).
선택을 비활성화하는 방법이 하나 더 있습니다.
<ListView ...>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel IsEnabled="False"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
같습니다
<ListView IsEnabled="False">
하지만 스크롤러가 비활성화되지 않은 경우
아래 코드는 ListView를 비활성화합니다.항목 행을 선택하고 패딩, 여백 등을 추가할 수도 있습니다.
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<ListViewItem Padding="0" Margin="0">
<ContentPresenter />
</ListViewItem>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
아래 코드 사용 안 함 목록 보기에 포커스 설정항목
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="Background" Value="Transparent" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<ContentPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<ListView Grid.Row="1" ItemsSource="{Binding Properties}" >
<!--Disable selection of items-->
<ListView.Resources>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Grid Background="{TemplateBinding Background}">
<Border Name="Selection" Visibility="Collapsed" />
<!-- This is used when GridView is put inside the ListView -->
<GridViewRowPresenter Grid.RowSpan="2"
Margin="{TemplateBinding Padding}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.Resources>
<ListView.View>
<GridView>
<GridViewColumn Width="90" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Width="90" CellTemplateSelector="{StaticResource customCellTemplateSelector}" />
</GridView>
</ListView.View>
</ListView>
와 유사한 또 다른 컨트롤ListView
그리고.ListBox
선택권을 제공하지 않는 것은ItemsControl
대신 그것을 사용하는 것을 고려합니다.
언급URL : https://stackoverflow.com/questions/1051215/wpf-listview-turn-off-selection
'programing' 카테고리의 다른 글
PostgreSQL "표 설명" (0) | 2023.05.29 |
---|---|
파이썬 3 정수 나눗셈 (0) | 2023.05.29 |
Bash를 사용하여 변수 파일을 참조하려면 어떻게 해야 합니까? (0) | 2023.05.29 |
커서를 대기 커서로 돌리게 하려면 어떻게 해야 합니까? (0) | 2023.05.29 |
jQuery 처음을 제외한 모든 테이블 행 삭제 (0) | 2023.05.29 |