WPF XBAP树视图无法选择子项

时间:2013-09-04 12:06:19

标签: wpf treeview xbap

使用带有TreeView控件的WPF XBAP应用程序。 Treeview具有自定义ItemContainerStyle并使用分层数据绑定。当我最小化运行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 个答案:

答案 0 :(得分:0)

  

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

Window类中,只要删除焦点或将焦点返回到Deactivated对象,就会调用ActivatedWindow个事件。 如果您的XBAP顶级Window对象中有类似事件,则可能能够使用它们。 [对不起,但MSDN目前已关闭,所以我无法验证这是否正确。]

如果您可以附加到Activated事件,那么每次重新关注应用程序时,您都可以刷新UI以恢复其全部功能。