WPF XBAP 树视图无法选择子项

Posted

技术标签:

【中文标题】WPF XBAP 树视图无法选择子项【英文标题】:WPF XBAP treeview unable to select child item 【发布时间】:2013-09-07 22:24:52 【问题描述】:

我正在使用带有 TreeView 控件的 WPF XBAP 应用程序。 Treeview 有一个自定义的 ItemContainerStyle 并使用 Hierarchical 数据绑定。当我最小化运行 XBAP 的浏览器并再次最大化它并单击 TreeView 中的一个项目时,该项目没有被选中,即。 SelectedItemChanged 事件处理程序未触发。 下面是我用于 TreeViewItem 的样式:

<Style x:Key="TreeViewItemFocusVisual">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Rectangle Margin="0,0,0,0"
                     Opacity="0"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="ExpandCollapseToggleStyle" TargetType="x:Type ToggleButton">
        <Setter Property="Focusable" Value="False"/>
        <Setter Property="Width" Value="10"/>
        <Setter Property="Height" Value="10"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="x:Type ToggleButton">
                    <Image x:Name="imgExpand" Source="Images/plus.png"/>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="True">
                            <Setter Property="Source" TargetName="imgExpand" Value="Images/minus.png"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="TreeViewItemStyle" TargetType="x:Type TreeViewItem">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="HorizontalContentAlignment" Value="Binding HorizontalContentAlignment, RelativeSource=RelativeSource AncestorType=x:Type ItemsControl"/>
        <Setter Property="VerticalContentAlignment" Value="Binding VerticalContentAlignment, RelativeSource=RelativeSource AncestorType=x:Type ItemsControl"/>
        <Setter Property="Padding" Value="1,0,0,0"/>
        <Setter Property="Foreground" Value="DynamicResource x:Static SystemColors.ControlTextBrushKey"/>
        <Setter Property="FocusVisualStyle" Value="StaticResource TreeViewItemFocusVisual"/>
        <Setter Property="IsExpanded" Value="Binding Path=IsItemExpanded" />
        <!--<Setter Property="IsSelected" Value="Binding Path=IsEntitySelected" />-->
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="x:Type TreeViewItem">
                    <Grid x:Name="grd">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <Rectangle x:Name="HorLn" Height="1" Margin="10,0,0,0" RenderOptions.EdgeMode="Aliased" Stroke="White"
                                   SnapsToDevicePixels="true" StrokeDashArray="1 2" StrokeThickness="1"/>
                        <Rectangle x:Name="VerLn" Width="1" Grid.RowSpan="2" RenderOptions.EdgeMode="Aliased" Stroke="White"
                                   SnapsToDevicePixels="true" StrokeDashArray="1 2" StrokeThickness="1"/>
                        <ToggleButton x:Name="Expander" Style="StaticResource ExpandCollapseToggleStyle" ClickMode="Press" 
                                      IsChecked="Binding IsExpanded, RelativeSource=RelativeSource TemplatedParent"/>                        
                        <StackPanel Orientation="Horizontal" Grid.Column="1" >
                            <!--<Image Width="16" Height="16" Margin="0" Source="Binding Path=ImageSource" x:Name="imgFlag"/>-->
                            <Border x:Name="Bd" SnapsToDevicePixels="true" Background="TemplateBinding Background" 
                                    BorderBrush="TemplateBinding BorderBrush" BorderThickness="TemplateBinding BorderThickness" Margin="0" Padding="0">
                                <ContentPresenter x:Name="PART_Header" HorizontalAlignment="TemplateBinding HorizontalContentAlignment" 
                                                  SnapsToDevicePixels="TemplateBinding SnapsToDevicePixels" ContentSource="Header"/>
                            </Border>
                        </StackPanel>
                        <ItemsPresenter x:Name="ItemsHost" Grid.Column="1" Grid.Row="1"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <DataTrigger Binding="Binding RelativeSource=RelativeSource Self, Converter=x:Static converters:TreeViewLineConverter.Instance" Value="true">
                            <Setter TargetName="VerLn"
                                Property="Height"
                                Value="1"/>
                            <Setter TargetName="VerLn"
                                Property="VerticalAlignment"
                                Value="Top"/>
                        </DataTrigger>
                        <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="#71a3ff"/>                            
                            <Setter Property="Control.Foreground" TargetName="Bd" Value="DynamicResource x:Static SystemColors.HighlightTextBrushKey"/>
                        </Trigger>
                        <Trigger Property="IsFocused" Value="True">
                            <Setter Property="Background" TargetName="Bd" Value="DynamicResource x:Static SystemColors.ControlBrushKey"/>
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected" Value="true"/>
                                <Condition Property="IsSelectionActive" Value="true"/>
                            </MultiTrigger.Conditions>                            
                            <Setter Property="Background" TargetName="Bd" Value="#71a3ff"/>
                            <Setter Property="Control.Foreground" TargetName="Bd" Value="DynamicResource x:Static SystemColors.ControlTextBrushKey"/>
                        </MultiTrigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected" Value="true"/>
                                <Condition Property="IsSelectionActive" Value="false"/>
                            </MultiTrigger.Conditions>                            
                            <Setter Property="Background" TargetName="Bd" Value="#71a3ff"/>
                            <Setter Property="Control.Foreground" TargetName="Bd" Value="DynamicResource x:Static SystemColors.ControlTextBrushKey"/>
                        </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="VirtualizingStackPanel.IsVirtualizing" Value="true">
                <Setter Property="ItemsPanel">
                    <Setter.Value>
                        <ItemsPanelTemplate>
                            <VirtualizingStackPanel/>
                        </ItemsPanelTemplate>
                    </Setter.Value>
                </Setter>
            </Trigger>            
        </Style.Triggers>
    </Style>

提前感谢您的帮助。

【问题讨论】:

【参考方案1】:

免责声明:这只是一个有根据的猜测

Window 类中,有DeactivatedActivated 事件在焦点被移除或返回到Window 对象时被调用。 如果您的 XBAP *** Window 对象中有类似的事件,您可能可以利用它们。 [对不起,MSDN 目前已关闭,因此我无法验证这是否正确。]

如果您可以附加到Activated 事件,那么您可以在每次应用程序再次获得焦点时刷新 UI 以恢复其全部功能。

【讨论】:

以上是关于WPF XBAP 树视图无法选择子项的主要内容,如果未能解决你的问题,请参考以下文章

选择但失去焦点时更改 WPF treeViewItem 背景颜色

WPF WBA (XBAP) 与 Silverlight

如何从 WPF 树视图中删除边框

部署完全信任的wpf浏览器应用程序(XBAP)

单击或选择 Clistctrl 子项(报告视图)

如何在 lync cwe 托管的 xbap 中获取当前对话?