Dot-Net

WPF功能區,選擇功能區選項卡時更改主要內容

  • October 31, 2019

點擊功能區選項卡時,我想在 WPF 應用程序中更改主表面的內容(功能區本身下方的內容)。我正在使用辦公室功能區,這並不重要。那麼我應該使用哪個 WPF 容器控制項,我該怎麼做呢?我應該只是隱藏可見性的各種控制項,還是什麼。我不是 WPF 專家,所以我需要一點靈感。

我先說我懷疑這是最好的方法。

這是我的 RibbonTab 樣式通知 IsSelected 綁定到視圖模型中的 IsSelected

 <!-- RibbonTab -->
       <Style TargetType="{x:Type ribbon:RibbonTab}">
           <Setter Property="ContextualTabGroupHeader" Value="{Binding ContextualTabGroupHeader}" />
           <Setter Property="Header" Value="{Binding Header}" />
           <Setter Property="ItemsSource" Value="{Binding GroupDataCollection}" />
           <Setter Property="IsSelected" Value="{Binding IsSelected}" />
       </Style>

這是視圖模型程式碼

   public bool IsSelected
   {
       get
       {
           return _isSelected;
       }

       set
       {
           if (_isSelected != value)
           {
               _isSelected = value;
               OnPropertyChanged(new PropertyChangedEventArgs("IsSelected"));
           }
       }
   }
   private bool _isSelected;

在 TabViewModel 的建構子中,我為內容的 ViewModel 取了一個參數

   public TabData(ISelectedContentTab content)
       : this(content.DisplayName)
   {
       _selectedContent = content;
   }

   private ISelectedContentTab _selectedContent;

然後我使用 ItemsControl 在我的 xaml 中顯示選定的內容

 <ItemsControl Grid.Row="1" VerticalContentAlignment="Stretch" VerticalAlignment="Stretch" 
                 ItemsSource="{Binding ElementName=ribbon,Path=SelectedItems}" 
                 ItemTemplate="{StaticResource ContentControlTemplate}" />

我擁有的 ContentControlTemplate 是

<DataTemplate x:Key="ContentControlTemplate">
           <Grid>
               <Grid.RowDefinitions>
                   <RowDefinition Height="*" />
               </Grid.RowDefinitions>
               <ContentControl Grid.Row="0" VerticalAlignment="Stretch" Height="Auto" VerticalContentAlignment="Stretch" Content="{Binding SelectedContent}" />
           </Grid>
       </DataTemplate>

還要確保你有一個數據模板將你的內容指向一個視圖

希望這可以幫助。

引用自:https://stackoverflow.com/questions/1846647