我曾经遇到了同样的问题,其中一个TreeView使用数据模板和多个用户控件显示层次结构项,其中一个用户控件是带有弹出窗口的切换按钮。
这会完全冻结并且完全没有响应。该问题与Focus属性有关,但设置Focusable为false
并不总是有效。
通过查看以上提供的解决方案,我喜欢最后一个使用ComboBox在TreeViewItem内部的解决方案。但它导致了另一个问题。每当用户选择组合框的(单个)项目时,它将显示在组合框中作为selectedValue
,而我只想像切换功能一样运行。
此外,我想将组合框上的三角形更改为省略号。因此,我尝试通过更改控件模板来解决此问题。
这是我的解决方案:
...
<Style x:Key="ComboBoxStyle1" TargetType="">
<Setter Property="FocusVisualStyle" Value=""/>
<Setter Property="Foreground" Value="}"/>
<Setter Property="Background" Value=""/>
<Setter Property="BorderBrush" Value=""/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="Padding" Value="4,3"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="">
<Grid x:Name="MainGrid" SnapsToDevicePixels="true">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Popup x:Name="PART_Popup" Margin="1"
AllowsTransparency="true"
IsOpen="}"
Placement="Bottom"
PopupAnimation="Fade">
<Microsoft_Windows_Themes:SystemDropShadowChrome x:Name="Shdw" MaxHeight="" MinWidth="" Color="Transparent">
<Border x:Name="DropDownBorder" Background="}" BorderBrush="}" BorderThickness="2" CornerRadius="0,4,4,4">
<ScrollViewer CanContentScroll="true">
<ItemsPresenter SnapsToDevicePixels="" KeyboardNavigation.DirectionalNavigation="Contained"/>
</ScrollViewer>
</Border>
</Microsoft_Windows_Themes:SystemDropShadowChrome>
</Popup>
<ToggleButton Style=""
Background=""
BorderBrush=""
IsChecked="}"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="HasDropShadow" SourceName="PART_Popup" Value="true">
<Setter Property="Margin" TargetName="Shdw" Value="0,0,5,5"/>
<Setter Property="Color" TargetName="Shdw" Value="#71000000"/>
</Trigger>
<Trigger Property="HasItems" Value="false">
<Setter Property="Height" TargetName="DropDownBorder" Value="95"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="}"/>
<Setter Property="Background" Value="#FFF4F4F4"/>
</Trigger>
<Trigger Property="IsGrouping" Value="true">
<Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsEditable" Value="true">
<Setter Property="BorderBrush" Value=""/>
<Setter Property="Background" Value="}"/>
<Setter Property="IsTabStop" Value="false"/>
<Setter Property="Padding" Value="3"/>
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="ComboBoxItemStyle1" TargetType="">
<Setter Property="HorizontalContentAlignment" Value="}}"/>
<Setter Property="VerticalContentAlignment" Value="}}"/>
<Setter Property="Padding" Value="3,0,3,0"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="">
<Border x:Name="Bd" SnapsToDevicePixels="true" Background="" BorderBrush="" BorderThickness="" Padding="">
<ContentPresenter HorizontalAlignment="" VerticalAlignment="" SnapsToDevicePixels=""/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsHighlighted" Value="true">
<Setter Property="Background" TargetName="Bd" Value="}"/>
<Setter Property="Foreground" Value="}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<TreeView >
<TreeViewItem IsExpanded="True">
<ComboBox HorizontalAlignment="Left" VerticalAlignment="Top"
IsEditable="False"
Style="">
<ComboBox.Resources>
<SolidColorBrush x:Key="" Color="Transparent"/>
</ComboBox.Resources>
<ComboBoxItem IsSelected="False">
<StackPanel Orientation="Vertical">
<Label>Some stuff on the combobox popup</Label>
<RadioButton>Radio Button 1</RadioButton>
<RadioButton>Radio Button 2</RadioButton>
<CheckBox>Check Box</CheckBox>
<Button HorizontalAlignment="Right">Ok</Button>
</StackPanel>
</ComboBoxItem>
</ComboBox>
</TreeViewItem>
</TreeView>