0

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

ViewModel Code:

private bool _isSelected = false;
public bool IsSelected
{
    get { return _isSelected; }
    set
    {
        if (_isSelected != value)
        {
            _isSelected = value;
            OnPropertyChanged("IsSelected");
        }
    }
}
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Thelonias
  • 2,918
  • 3
  • 29
  • 63

1 Answers1

0

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");
      }
    }
}
CodeWarrior
  • 7,388
  • 7
  • 51
  • 78
  • I would do this, but unless I'm missing something obvious, I can't do this...IsKeyboardFocusWithin is a read-only property on UIElement – Thelonias Jun 27 '11 at 14:44
  • 1
    Ack! I had not realized that. I looked up binding readonly properties to readwrite properties and came up only with the following SO question as viable. It is unclear which resolution actually works the way the OP wanted... http://stackoverflow.com/questions/658170/onewaytosource-binding-from-readonly-property-in-xaml – CodeWarrior Jun 27 '11 at 15:06
  • After further inspection, I don't think IsKeyboardFocusWithin is going to be my best bet. My TreeListViewItems can have items, and I'm running into an issue where if I give focus to a textbox in a child TreeListViewItem, my parent item get's selected. I used a variation of the "solution" in the link you provided, but it's still not what I'm looking for. I think it works best for flat lists. – Thelonias Jun 27 '11 at 18:24