2012-09-26 136 views
1

我已经定义为一个ListBoxItem的以下式:如何从DataTemplate绑定到Selector.IsSelectionActive?

<Style x:Key="detailListBoxItemStyle" TargetType="{x:Type ListBoxItem}"> 
    <Setter Property="AutomationProperties.AutomationId" Value="{Binding Path=StringTableKey}"/> 
    <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="2,0,0,0"/> 
    <Setter Property="FocusVisualStyle" Value="{x:Null}" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
       <Border x:Name="Bd" Margin="4" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true"> 
        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
       </Border> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsSelected" Value="true"> 
         <Setter Property="Background" TargetName="Bd" Value="{DynamicResource MenuItemSelectedBackgroundBrush}"/> 
         <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/> 
        </Trigger> 
        <Trigger Property="IsSelected" Value="false"> 
         <Setter Property="Background" TargetName="Bd" Value="{DynamicResource MenuItemUnSelectedBackgroundBrush}"/> 
        </Trigger> 
        <!-- This is the case when a detail is selected (the master list loses focus). --> 
        <MultiTrigger> 
         <MultiTrigger.Conditions> 
          <Condition Property="IsSelected" Value="true"/> 
          <Condition Property="Selector.IsSelectionActive" Value="false"/> 
         </MultiTrigger.Conditions> 
         <!--<Setter Property="Opacity" TargetName="Bd" Value=".4"/>--> 
        </MultiTrigger> 
        <Trigger Property="IsEnabled" Value="false"> 
         <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

指此样式应用被定义为列表框:

<ListBox 
    x:Name="listBox" 
    Margin="0,60,0,0" 
    MaxHeight="600" 
    Foreground="Transparent" 
    Style="{StaticResource detailListBoxStyle}" 
    ItemContainerStyle="{StaticResource detailListBoxItemStyle}" 
    ItemsSource="{Binding Source={StaticResource detailCollectionViewSource}}" 
    ItemTemplateSelector="{StaticResource detailDataTemplateSelector}" 
    TouchDown="ListBoxTouchDown" 
    TouchMove="ListBoxTouchMove" 
    TouchUp="ListBoxTouchUp" 
    KeyDown="ListBoxKeyDown"> 
    <ListBox.GroupStyle> 
     <GroupStyle> 
      <GroupStyle.HeaderTemplate> 
       <DataTemplate> 
        <TextBlock Margin="0,10,0,0" FontWeight="Bold" FontSize="20" Foreground="White" Text="{Binding Path=Name}"/> 
       </DataTemplate> 
      </GroupStyle.HeaderTemplate> 
     </GroupStyle> 
    </ListBox.GroupStyle> 
</ListBox> 

我有一个ListBoxItem的一个DataTemplate,可以是:

<DataTemplate x:Key="detailOnOffTemplate"> 
    <Grid Height="50" Width="{StaticResource detailWidth}"> 
     <Grid.RowDefinitions> 
      <RowDefinition/> 
      <RowDefinition/> 
     </Grid.RowDefinitions> 
     <TextBlock x:Name="tb1" Margin="4,2,0,0" Grid.Row="0" Style="{StaticResource MenuTextStyle}" Text="{Binding DisplayName}" VerticalAlignment="Top" TextAlignment="Left"> 
     <TextBlock.Effect> 
      <DropShadowEffect Color="White" ShadowDepth="0" BlurRadius="7"/> 
     </TextBlock.Effect> 
     </TextBlock> 
    </Grid> 
    <DataTemplate.Triggers> 
     <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}, Mode=FindAncestor}}" Value="True"> 
      <Setter TargetName="tb1" Property="Foreground" Value="White"/> 
      <Setter TargetName="tb1" Property="Effect" Value="{x:Null}"/> 
     </DataTrigger> 
    </DataTemplate.Triggers> 
</DataTemplate> 

我需要能够从我的DataTemplate绑定到“Selector.IsSelectionActive”,但没有任何工作。我尝试过这些事情:

  <DataTrigger Binding="{Binding Selector.IsSelectionActive, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}, Mode=FindAncestor}}" Value="True"> 

      <Trigger Binding="{Binding Selector.IsSelectionActive, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}, Mode=FindAncestor}}" Value="True"> 

      <Trigger "Selector.IsSelectionActive" Value="True"> 

基本上,我想这是包含在ControlTemplate中在我的DataTemplate同一触发器:

    <MultiTrigger> 
         <MultiTrigger.Conditions> 
          <Condition Property="IsSelected" Value="true"/> 
          <Condition Property="Selector.IsSelectionActive" Value="false"/> 
         </MultiTrigger.Conditions> 
         <!--<Setter Property="Opacity" TargetName="Bd" Value=".4"/>--> 
        </MultiTrigger> 

或者说,我还能怎么知道项目“IsSelected”但没有键盘焦点?

回答

6

您尝试的第一个选项是正确的例外,您不能识别它是附加属性,因此它看起来像您试图绑定到名为Selector的属性,该属性返回具有属性名称IsSelectionActive的对象。所以,我觉得像下面将工作:

<DataTrigger Binding="{Binding Path=(Selector.IsSelectionActive), RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" Value="True"> 
+1

工作,谢谢。那么,括号指定了一个附加属性? – throop77

+0

是的,因为您需要提供他们需要的类名称,以将该信息与属性名称进行分组。我相信这是在有关[PropertyPath](http://msdn.microsoft.com/en-us/library/system.windows.data.binding.path.aspx)的文档中提到的。 – AndrewS

0

以前的答案并没有为我工作,但许多小时的玩这种没有后:

这里的关键(我认为)是首先是IsSelectionActive FALSE。这个触发器也是一个与IsSelected结合的多触发器。在这种情况下,我不必使用括号。但那可能是因为我使用的是最新的.net框架,Wpf和Visual Studio,因此您可能需要上一个答案中提到的括号。

红色,黄色和白色仅用于此示例,请记住更改这些颜色。

<ControlTemplate.Triggers> 
    <MultiTrigger> 
     <!-- selected, but not focused --> 
     <MultiTrigger.Conditions> 
      <Condition Property="Selector.IsSelectionActive" Value="False" /> 
      <Condition Property="IsSelected" Value="True" /> 
     </MultiTrigger.Conditions> 
     <Setter Property="Background" TargetName="Bd" Value="Yellow" /> 
    </MultiTrigger> 
    <MultiTrigger> 
     <!-- selected, and focused --> 
     <MultiTrigger.Conditions> 
      <Condition Property="Selector.IsSelectionActive" Value="True" /> 
      <Condition Property="IsSelected" Value="True" /> 
     </MultiTrigger.Conditions> 
     <Setter Property="Background" TargetName="Bd" Value="Red" /> 
     <Setter Property="Foreground" Value="White" /> 
    </MultiTrigger>      
</ControlTemplate.Triggers>