1

我正在使用带有 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>

感谢您提前提供任何帮助。

4

1 回答 1

0

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

Window类中,每当焦点被移除或返回到对象时,都会调用Deactivated和事件。如果您的 XBAP 顶级对象中有类似的事件,您可以利用它们。[对不起,但 MSDN 目前已关闭,因此我无法验证这是否正确。]ActivatedWindowWindow

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

于 2013-09-04T12:32:36.207 回答