我看到有方法可以禁用ListView
中的選擇,但我還沒有看到TreeView
或Grid
的任何內容。在TreeView中禁用選擇
我的形式是很基本的,看起來像這樣:
問題是有人可以去點擊其中任何一個標籤,突然它看起來像這樣:
如何禁用此行爲?
<TreeView Focusable="False">
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="Focusable" Value="False"/>
</Style>
</TreeView.ItemContainerStyle>
<TreeViewItem Focusable="False">
<TreeViewItem.Header>Group Header</TreeViewItem.Header>
<TreeViewItem.IsExpanded>true</TreeViewItem.IsExpanded>
<Grid Focusable="False">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Content="Message ID">
<Grid.Row>0</Grid.Row>
<Grid.Column>0</Grid.Column>
</Label>
<TextBox Text="{Binding Path=GroupHeader.MessageId}" HorizontalAlignment="Stretch">
<Grid.Column>1</Grid.Column>
<Grid.Row>0</Grid.Row>
</TextBox>
<Label Content="Creation Date">
<Grid.Row>1</Grid.Row>
<Grid.Column>0</Grid.Column>
</Label>
<DatePicker SelectedDate="{Binding Path=GroupHeader.CreationDateTime}">
<Grid.Row>1</Grid.Row>
<Grid.Column>1</Grid.Column>
</DatePicker>
<Label Content="Instructing Agent">
<Grid.Row>2</Grid.Row>
<Grid.Column>0</Grid.Column>
</Label>
<TextBox Text="{Binding Path=GroupHeader.InstructingAgent}">
<Grid.Row>2</Grid.Row>
<Grid.Column>1</Grid.Column>
</TextBox>
<Label Content="Instructed Agent">
<Grid.Row>3</Grid.Row>
<Grid.Column>0</Grid.Column>
</Label>
<TextBox Text="{Binding Path=GroupHeader.InstructedAgent}">
<Grid.Row>3</Grid.Row>
<Grid.Column>1</Grid.Column>
</TextBox>
</Grid>
</TreeViewItem>
<TreeViewItem>
...
</TreeViewItem>
</TreeView>
使用TreeView.ItemContainerStyle
只是禁用它在實際的項目頭,而不是在它的內容似乎。
更新:我試着從克里斯·W上的評論和有關設置控件模板的回答表明,使用以下:
<Style x:Key="StretchingTreeViewItemStyle1" TargetType="{x:Type local:StretchingTreeViewItem}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="Padding" Value="1,0,0,0"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="FocusVisualStyle">
<Setter.Value>
<Style>
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:StretchingTreeViewItem}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="19" Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<ToggleButton x:Name="Expander" ClickMode="Press" IsChecked="{Binding IsExpanded, RelativeSource={RelativeSource TemplatedParent}}">
<ToggleButton.Style>
<Style TargetType="{x:Type ToggleButton}">
<Setter Property="Focusable" Value="False"/>
<Setter Property="Width" Value="16"/>
<Setter Property="Height" Value="16"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border Background="Transparent" Height="16" Padding="5" Width="16">
<Path x:Name="ExpandPath" Data="M0,0 L0,6 L6,0 z" Fill="Transparent" Stroke="#FF989898">
<Path.RenderTransform>
<RotateTransform Angle="135" CenterY="3" CenterX="3"/>
</Path.RenderTransform>
</Path>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="RenderTransform" TargetName="ExpandPath">
<Setter.Value>
<RotateTransform Angle="180" CenterY="3" CenterX="3"/>
</Setter.Value>
</Setter>
<Setter Property="Fill" TargetName="ExpandPath" Value="#FF595959"/>
<Setter Property="Stroke" TargetName="ExpandPath" Value="#FF262626"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Stroke" TargetName="ExpandPath" Value="#FF1BBBFA"/>
<Setter Property="Fill" TargetName="ExpandPath" Value="Transparent"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True"/>
<Condition Property="IsChecked" Value="True"/>
</MultiTrigger.Conditions>
<Setter Property="Stroke" TargetName="ExpandPath" Value="#FF262626"/>
<Setter Property="Fill" TargetName="ExpandPath" Value="#FF595959"/>
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ToggleButton.Style>
</ToggleButton>
<Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Column="1" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True">
<ContentPresenter Focusable="False" x:Name="PART_Header" ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" ContentStringFormat="{TemplateBinding HeaderStringFormat}" ContentSource="Header" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
<ItemsPresenter Focusable="False" x:Name="ItemsHost" Grid.ColumnSpan="2" Grid.Column="1" Grid.Row="1"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsExpanded" Value="False">
<Setter Property="Visibility" TargetName="ItemsHost" Value="Collapsed"/>
</Trigger>
<Trigger Property="HasItems" Value="False">
<Setter Property="Visibility" TargetName="Expander" Value="Hidden"/>
</Trigger>
<!--<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="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>
<Style.Triggers>
<Trigger Property="VirtualizingPanel.IsVirtualizing" Value="True">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<VirtualizingStackPanel IsItemsHost="True"/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
結果是對TreeViewItem
本身的文字不再是可選/不會突出顯示,但其中的內容仍然可以。
我試過設置一切我能想到的Focusable="False"
,但那也行不通,所以我開始認爲它不關注焦點在一個項目上。特別是因爲如果你嵌套TreeViews
你可以同時選擇一個內部和一個外部項目。
它將在默認控制模板中作爲選擇項目樣式的嵌套ToggleButton上的多觸發器。我似乎無法找到一個默認的WPF樹視圖項目樣式,並且我懶得去製作一個wpf proj只是爲了拉出一個,但如果你想共享一個,很容易發現它在那裏。 –