在功能区内使用UserControl

时间:2014-07-15 11:28:55

标签: wpf user-controls ribbon

我想在RibbonControl中使用UserControl。

例如:

<RibbonWindow
  xmlns:uc="clr-namespace:UserControl1;assembly=UserControl1">

  <Grid ShowGridLines="False" Margin="0">
     <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="100*"/>
     </Grid.RowDefinitions>
     <Grid.ColumnDefinitions>
        <ColumnDefinition Width="100*"/>
     </Grid.ColumnDefinitions>

     <Ribbon Grid.Row="0">
        <RibbonApplicationMenu >
          <RibbonApplicationMenuItem x:Name="miExit" ImageSource="Images/large_exit.png" Header="Exit" />
        </RibbonApplicationMenu>
        <RibbonTab Header="Test">
            <uc:RibbonGroups/>
        </RibbonTab>
     </Ribbon>

     <uc:Content Grid.Row="1" />

  </Grid>

</RibbonWindow>

您会看到两个UserControl,uc:RibbonGroups和uc:Content。虽然第二个(uc:Content)工作正常,但RibbonControl中的UserControl将无法工作。

我错过了什么? 我也尝试这种方法: How to set the usercontrol for ribbon window in WPF? 在RibbonTab中设置一个网格,然后设置UserControl;

     <Ribbon Grid.Row="0">
         ...
        <Grid>
        <RibbonTab Header="Test">
            <uc:RibbonGroups/>
        </RibbonTab>
        </Grid>
     </Ribbon>

而不是

     <Ribbon Grid.Row="0">
         ...
        <RibbonTab Header="Test">
            <uc:RibbonGroups/>
        </RibbonTab>
     </Ribbon>

修改 UserControl现在只保存RibbonTab的内容,RibbonTab本身移动到主窗口

  <UserControl ...>
     <StackPanel Orientation="Horizontal">
        <RibbonGroup Header="Test" >
             <RibbonButton SmallImageSource="Images/small_save.png"
                           LargeImageSource="Images/large_save.png" />
        <Image Source="Images/large_expert_enabled.png"></Image>
     </Stackpanel>  
  </UserControl>

RibbonGroup显示为Header但没有图像,并且单独的Image会自动显示。那么也许现在有什么建议出了什么问题?

EDIT2: 如果我进入Snoop,会出现RibbonButtons及其相应的图像。

EDIT3: 我尝试了另一种资源方式,但同样的问题如上所述.. 它看起来如何,当我在资源/ UserControl中有RibbonTab时: http://s12.postimg.org/lpposl48t/DLL_Ribbon_Tab_fail_part.png

当我在资源/ UserControl中拥有RibbonGroup时,它看起来如何: http://s12.postimg.org/oiiwcm4l9/DLL_Ribbon_Group_fail_part.png

1 个答案:

答案 0 :(得分:0)

经过大量研究后,我从Ben Barefield找到了这个:

http://www.benbarefield.com/blog/2011/03/04/ribbon-tab-definition-on-usercontrol/

他使用技巧并将ribbontab移动到resourcedictionary中。 运行良好,但我也想在这个ribbontab中添加一些点击事件(具有视图特定的操作....)所以我决定回到usercontrol。

诀窍是,在将ribbontab添加到其他控件之前将其从用户控件中分离出来。

详细说明:

将此方法添加到usercontrol代码隐藏:

    private void AttachTabToRibbon(RibbonTab tab)
    {
        Ribbon Ribbon = null;
        Ribbon = getRibbon();
        if (Ribbon == null)
            return;
        Ribbon.Items.Add(tab);
    }

    private Ribbon getRibbon()
    {
        var control = VisualTreeHelper.GetParent(this);
        Ribbon Ribbon = null;
        while (control != null && Ribbon == null)
        {
            var numberOfChildren = VisualTreeHelper.GetChildrenCount(control);
            for (int i = 0; i < numberOfChildren; ++i)
            {
                var child = VisualTreeHelper.GetChild(control, i);
                if (child is Ribbon)
                {
                    Ribbon = child as Ribbon;
                    return Ribbon;
                }
            }
            control = VisualTreeHelper.GetParent(control);
        }
        return null;
    }

    private void userControl_Loaded(object sender, RoutedEventArgs e)
    {
        Ribbon ribbonMenu = (Ribbon)this.FindName("ribbon");
        RibbonTab ribbonTabAdminInterface = (RibbonTab)this.FindName("ribbonTab");
        ribbonMenu.Items.Remove(ribbonTab);
        ucStackPanel.Children.Remove(ribbonMenu);
        AttachTabToRibbon(ribbonTab);
    }

进一步调用userControl_Loaded:

<UserControl ...
             Loaded="userControl_Loaded"
>
<StackPanel x:Name="ucStackPanel">
    <Ribbon x:Name="ribbon" >
        <Ribbon.ApplicationMenu>
            <RibbonApplicationMenu
            </RibbonApplicationMenu>
        </Ribbon.ApplicationMenu>
        <RibbonTab x:Name="ribbonTab"/>
        ...
     </Ribbon>
     ...
</StackPanel>