代码之家  ›  专栏  ›  技术社区  ›  Klaus Byskov Pedersen

WPF ribbon,选择ribbontab时更改主要内容

  •  8
  • Klaus Byskov Pedersen  · 技术社区  · 16 年前

    我想在单击功能区选项卡时更改WPF应用程序中主曲面(功能区本身下方的内容)的内容。我用的是办公室的缎带,这不重要。那么,我应该使用哪个WPF容器控件,以及如何使用它呢?我应该隐藏各种控件的可见性吗。我不是WPF专家,所以我需要一些灵感。

    4 回复  |  直到 14 年前
        1
  •  11
  •   Wegged    16 年前

    我先说我怀疑这是最好的方法。

      <!-- 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;
    

        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}" />
    

     <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>
    

    还要确保有一个将内容指向视图的数据模板

    希望这有帮助。

        2
  •  8
  •   StayOnTarget Charlie Flowers    7 年前

    这样做的目的是将功能区下方的内容分层堆叠(如在Photoshop或任何其他图形编辑器中),并在此时仅显示您需要的层。捆绑 Visibility 你的层的 IsSelected

    这里的MainGrid是层(也是网格)的容器:

        <Grid x:Name="MainGrid">
            <Grid Visibility="{Binding IsSelected, Converter={StaticResource BooleanToVisibilityConverter}, ElementName=ribbonTab2}">
                <Image x:Name="ImgMain" Source="x.jpg"/>
            </Grid>
            <Grid Visibility="{Binding IsSelected, Converter={StaticResource BooleanToVisibilityConverter}, ElementName=ribbonTab1}">
                <Image x:Name="ImgXtra" Source="y.jpg"/>
           </Grid>
        </Grid>
    

    添加 <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" /> 在资源方面。

    …而且你根本不需要任何代码!

        3
  •  2
  •   Nawed Nabi Zada    13 年前

    <Frame NavigationUIVisibility="Hidden" x:Name="FrmMainFrame" DockPanel.Dock="Bottom"/>
    

    在你的代码背后:

    mainWindowView.RibMain.SelectionChanged += RibMain_SelectionChanged;
    
    void RibMain_SelectionChanged(object sender,  System.Windows.Controls.SelectionChangedEventArgs e)
        {
            var tab = this.mainWindowView.RibMain.SelectedItem as RibbonTab;
            if (tab.Header.Equals("Explorer"))
            {
                mainWindowView.FrmMainFrame.Navigate(explorerController.View());
            }
            else
                mainWindowView.FrmMainFrame.NavigationService.Navigate(new Uri("http://www.google.com/"));
        }
    
        4
  •  1
  •   StayOnTarget Charlie Flowers    7 年前

    我发现了一个解决方案:

    给你的RibbonTab起个名字,这样你就可以在代码背后处理它了。 我知道有多种方法可以添加视图和控件,但以下是我所做的。。。 我只是在功能区后的主栅格中为视图添加了一个新栅格。即:

    <r:RibbonWindow>
      <Grid>
        <r:Ribbon>
          <r:RibbonTab Name="Tab1" Header="Home">
            <r:RibbonGroup Name="Group1">
              <r:RibbonButton LargeImageSource="images\icon.png" Label="Click Me"/>
            </r:RibbonGroup>
          </r:RibbonTab>
          <r:RibbonTab Name="Tab2" Header="Other Tab">
            <r:RibbonGroup Name="Group2">
              <r:RibbonButton LargeImageSource="images\icon.png" Label="Click Me"/>
            </r:RibbonGroup>
          </r:RibbonTab>
        </r:Ribbon>
        <Grid Name="Tab1RTB" Grid.Row="1" Visibility="Hidden">
          <RichTextBox Margin="5" BorderBrush="LightGray" BorderThickness="1"/>
        </Grid>
        <Grid Name="Tab2RTB" Grid.Row="1" Visibility="Hidden">
          <RichTextBox Margin="5" BorderBrush="LightGray" BorderThickness="1"/>
        </Grid>
      </Grid>
    </r:RibbonWindow>
    

    然后是代码隐藏(VB.NET)

    Private Sub TabChanged(sender As System.Object, e As SelectionChangedEventArgs) Handles ribbonHome.SelectionChanged
      If Tab1.IsSelected = True Then
        Tab1RTB.Visibility = Windows.Visibility.Visible
        Tab2RTB.Visibility = Windows.Visibility.Collapsed
      ElseIf Tab2.IsSelected = True
        Tab1RTB.Visibility = Windows.Visibility.Collapsed
        Tab2RTB.Visibility = Windows.Visibility.Visible
      Else
        Tab1RTB.Visibility = Windows.Visibility.Collapsed
        Tab2RTB.Visibility = Windows.Visibility.Collapsed
      End If
    End Sub