2015-12-10 177 views
1

我有一個GroupBox。此框包含一個UserControl動態更改GroupBox的內容

<GroupBox Header="NewsBox"> 

    <GroupBox.Content> 
     <NewsDay:NewsDayControl DataContext="{Binding SelectedNews}"/> 
    </GroupBox.Content> 

</GroupBox> 

現在,我要動態地取決於TabControl的選擇Tab鍵來改變GroupBox的內容。

<DataTrigger Binding="{Binding ElementName=TabControl, Path=SelectedIndex}" Value="0"> 
    <Setter Property="Visibility" Value="Hidden" /> 
</DataTrigger> 

這是應該出現在其他UserControl,如果選擇與指數1標籤:

<Imprint:ImprintControl DataContext="{Binding SelectedImprint}"/> 

我怎麼能這樣做?

回答

3

您可以使用Style這樣的觸發器。

<GroupBox Header="NewsBox"> 
    <GroupBox.Style> 
     <Style TargetType="GroupBox"> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding ElementName=TabControl, Path=SelectedIndex}" Value="0"> 
        <Setter Property="Content"> 
         <Setter.Value> 
          <NewsDay:NewsDayControl DataContext="{Binding SelectedNews}"/> 
         </Setter.Value> 
        </Setter> 
       </DataTrigger> 
       <DataTrigger Binding="{Binding ElementName=TabControl, Path=SelectedIndex}" Value="1"> 
        <Setter Property="Content"> 
         <Setter.Value> 
          <Imprint:ImprintControl DataContext="{Binding SelectedImprint}"/> 
         </Setter.Value> 
        </Setter> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </GroupBox.Style> 
</GroupBox>