ListViewItem IsKeyboardFocusWithin不在视图模型上触发IsSelectedChange

时间:2011-06-27 14:28:25

标签: wpf xaml binding styles

我有TreeListViewItem的样式......实际上只是“树结构”中的ListViewItemDataContext的{​​{1}}设置为我的视图模型,其ListViewItem属性绑定到项IsSelected。我进行了更改,以便在选择项目时,以不同方式设置项目样式,并将viewmodel上的属性设置为“true”。这很好用,但是当我为IsSelected添加了一个触发器时,viewmodel属性不再被设置。项目的样式仍然会更改,但我还需要在viewmodel上更改属性。任何帮助都会很棒。

XAML:

IsKeyboardFocusWithin

ViewModel代码:

<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"/>                                                        
                          <Setter 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>

1 个答案:

答案 0 :(得分:0)

为什么不在ViewModel中创建一个KeyboardFocus属性并将逻辑放在那里?

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");
      }
    }
}