2011-01-06 72 views
0

我有一個ListBox指定ItemContainerStyle。 ItemContainerStyle使用觸發器來設置ContentTemplate。如何在應用模板時設置數據模板成員的可見性?

我想在應用模板時在ContentTemplate中設置ContentControl的可見性(名爲「ExpanderContent」)。如果可能,我寧願使用ListBoxItem上附加屬性的值。

也許這將有助於使問題更清楚地看到我嘗試使用應用於ContentControl的Style(名爲「ExpanderContent」)的一個示例。我意識到Style未在下面的代碼中的ContentControl上設置。我已經確定並應用了它,並且看到解決附加屬性時沒有錯誤。

<ListBox ItemContainerStyle="{StaticResource lbcStyle}"/> 

<Style TargetType="ListBoxItem" x:Key="lbcStyle"> 
    <Style.Triggers> 
    <Trigger Property="IsSelected" Value="True"> 
     <Setter Property="ContentTemplate" Value="{StaticResource editable}"/> 
    </Trigger> 
    </Style.Triggers> 
    <Setter Property="ContentTemplate" Value="{StaticResource nonEditable}"/> 
</Style> 

<DataTemplate x:Key="nonEditable"> 
    <Grid Width="Auto" Height="Auto"> 
    ... 
    <ContentControl Name="ExpanderContent" Visibility="Collapsed" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="12"></ContentControl> 
    </Grid> 
</DataTemplate> 
<DataTemplate x:Key="editable"> 
    <Grid x:Name="grdEditable" Width="Auto" Height="Auto"> 
    ... 
    <ContentControl Name="ExpanderContent" Visibility="Collapsed" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="12"></ContentControl> 
    </Grid> 
</DataTemplate> 

<Style x:Key="editorContentControl" TargetType="{x:Type ContentControl}"> 
    <Style.Triggers> 
     <DataTrigger Binding="{Binding (local:AttachedProperties.IsExpanded),RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}}}" Value="True"> 
      <Setter Property="Visibility" Value="Visible"/> 
     </DataTrigger> 
     <DataTrigger Binding="{Binding (local:AttachedProperties.IsExpanded),RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}}}" Value="False"> 
      <Setter Property="Visibility" Value="Collapsed"/> 
     </DataTrigger> 
    </Style.Triggers> 
    <Setter Property="Visibility" Value="Visible"/> 
</Style> 

這個廢話的真實世界的應用是我有一個列表框,並且列表框中的項目有選擇時會改變的模板。模板有一個擴展器,即使未選擇該項目,我也想讓擴展器保持擴展狀態。就像現在一樣,每當我更改列表框選擇時,內容模板都會以其原始狀態重新應用,該狀態已摺疊 - 因此,我一次只能放置一個以上的內容模板。

alt text

回答

1

最簡單的解決方案,而不是試圖通過周圍不同的模板之間的視覺狀態值,只是使用單一的DataTemplate你的ContentTemplate,只是使用觸發器在模板中顯示和隱藏相應的控件爲當前選擇狀態。這樣,每次切換狀態時都不會取代擴展器控制。

+0

事實上,這會更簡單,但需要大量重寫。 – 2011-01-06 20:31:58

相關問題