开发者

ListViewItem IsKeyboardFocusWithin not triggering IsSelectedChange on view model

I have a Style for a TreeListViewItem...really just a ListViewItem in a "tree structure". The DataContext of the ListViewItem is set to my view model that has an IsSelected property bound to the item's IsSelected. I made a change so that when the item is selected, style the item differently, and the property on my viewmodel gets set to 'true'. This works well, but when I added a trigger for IsKeyboardFocusWithin, the viewmodel property no longer gets set. The style of the item still changes, but I need the property on the viewmodel to change as well. Any help would be great.

XAML:

<Style TargetType="{x:Type zcorectl:ZynTreeListViewItem}">
    <Setter Property="KeyboardNavigation.TabNavigation" Value="Continue"/>
    <Setter Property="IsExpanded" Value="{Binding Path=IsExpanded, Mode=TwoWay}"/>
    <Setter Property="IsSelected" Value="{Binding Path=IsSelected, UpdateSourceTrigger=PropertyChanged}"/>
    <Setter Property="Template">
        <Setter.Value>
             <ControlTemplate TargetType="{x:Type zcorectl:ZynTreeListViewItem}">
                <StackPanel>
                    <Border x:Name="Bd" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
                        <GridViewRowPresenter x:Name="PART_Header" Content="{TemplateBinding Header}" Columns="{StaticResource columns}" />
                    </Border>
                    <ItemsPresenter x:Name="ItemsHost"/>
                </StackPanel>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsExpanded" Value="false">
                        <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed"/>
                    </Trigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="HasHeader" Value="false"/>
                            <Condition Property="Width" Value="Auto"/>
                        </MultiTrigger.Conditions>
                        <Setter TargetName="PART_Header" Property="MinWidth" Value="75"/>
                    </MultiTrigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="HasHeader" Value="false"/>
                            <Condition Property="Height" Value="Auto"/>
                        </MultiTrigger.Conditions>
                        <Setter TargetName="PART_Header" Property="MinHeight" Value="19"/>
                     </MultiTrigger>
                     <Trigger Property="IsKeyboardFocusWithin" Value="True">
                          <Setter Property="IsSelected" Value="True"/>
                     </Trigger>
                     <Trigger Property="IsSelected" Value="true">
                         <Setter TargetName="Bd" Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/> 
                         <Setter TargetName="Bd" Property="BorderThickness" Value="0,2"/>                                                        
                         <Setter TargetName="Bd" Property="Height" Value="45"/>                                                        
                         <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 TargetName="Bd" Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                          <Setter TargetName="Bd" Property="BorderThickness" Value="0,2"/>                                                        
                          <Setter TargetName="Bd" Property="Height" Value="45"/>                                                        
                          <Sette开发者_C百科r Property="Foreground" 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>

ViewModel Code:

private bool _isSelected = false;
public bool IsSelected
{
    get { return _isSelected; }
    set
    {
        if (_isSelected != value)
        {
            _isSelected = value;
            OnPropertyChanged("IsSelected");
        }
    }
}


Why not create a KeyboardFocus property in the ViewModel and put the logic there?

private bool _isKeyboardFocusSet = false;
public bool IsKeyboardFocusSet
{
   get { return _isKeyboardFocusSet; }
   set
    {
      if (_isKeyboardFocusSet!= value)
      {
         _isKeyboardFocusSet = value;
         OnPropertyChanged("isKeyboardFocusSet");
      }
      if (_isSelected != true)
      {
         _isSelected = _isKeyboardFocusSet;
         OnPropertyChanged("IsSelected");
      }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜