2012-09-26 81 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>