2012-03-28 21 views
0

我需要将我的WPF应用程序(具有Ribbon)转换为使用矢量图形处理按钮等上的图像,而不是位图。到目前为止,我的工作已经完成,除了RibbonApplicationMenu项目(使用RibbonApplicationMenu.ImageSource属性)和ImageSource设置显示在功能区菜单项左侧的图形之外。如何将矢量图形(在XAML中定义)用作Ribbon菜单项的图像?

我的矢量图形的定义是这样的XAML:但是

<ControlTemplate xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <ViewBox Width="148.854" Height="150.500"> 
     <Canvas Width="148.854" Height="150.500"> 
      <Path> 
      <!-- . . . yadda yadda yadda . . . --> 
      </Path> 
     </Canvas> 
    <Viewbox> 
</ControlTemplate> 

这些渲染正确,当我将它们设置为是Button.Template

我想不通, ,如何将它们用于RibbonApplicationMenu控件中显示的图像。只需设置RibbonApplicationMenu.Template属性不起作用(它填充整个控件似乎打破其功能)。

我试图使用VisualBrush然后渲染到一个RenderTargetBitmap作为使用.ImageSource(我也有在代码中大多作品背后的原因,我不会进入):

ContentControl cc = new ContentControl(); // just picked ContentControl as a test 
cc.Template = myTemplate; // assume myTemplate has the proper ControlTemplate 
VisualBrush visBrush = new VisualBrush(); 
visBrush.Visual = cc; 
// not really sure about the parameters to this next line 
RenderTargetBitmap rend = 
    new RenderTargetBitmap(148.854, 150.5, 120, 96, PixelFormats.Pbgra32); 
rend.Render(cc); 
RibbonApplicationMenuItem menu = new RibbonApplicationMenuItem(); 
menu.ImageSource = render; 

这编译,但只显示图像将去的空白。如果我使用从图像文件加载的BitmapImage作为.ImageSource,它会正确显示。

任何想法,我可以得到这个工作?要做到这一点可能需要

回答

0

两件事情:首先 没有明确的尺寸在ControlTemplateCanvas设置,你需要指定它的宽度和高度(见the upvoted answer here了解详细信息)。 另一件事是,你需要ArrangeMeasureViewbox,以便它采取所需的尺寸。

所以改变了矢量图形的XAML喜欢的东西:

<ControlTemplate x:Key="Template"> 
    <Viewbox> 
     <Canvas Height="100" Width="130"> 
      <Path> 
       <!-- data --> 
      </Path> 
     </Canvas> 
    </Viewbox> 
</ControlTemplate> 

而且代码:

ControlTemplate controlTemplate = 
    FindResource("Template") as ControlTemplate; 

ContentControl content = new ContentControl(); 
content.Template = controlTemplate; 

// ensure control has dimensions 
content.Measure(new Size(200, 200)); 
content.Arrange(new Rect(0, 0, 200, 200)); 

RenderTargetBitmap render = 
    new RenderTargetBitmap((int)content.ActualWidth, (int)content.ActualHeight, 120, 96, PixelFormats.Pbgra32); 

render.Render(content); 

RibbonApplicationMenuItem menu = new RibbonApplicationMenuItem(); 
menu.ImageSource = render; 

将有希望得到它的工作。

+0

对不起,我应该在我的代码中包含更多的细节。我确实为画布设定了一个尺寸(并且我在我的问题中编辑了这些属性)。我会尽量测量和排列。 – Pysul 2012-03-30 14:08:51

+0

使用'.Measure'和'.Arrange'将功能显示在功能区中。我不确定它是否正是我所需要的,但它工作。尽管如此,它并没有正确排列,但我可能会摆弄它。 – Pysul 2012-03-30 14:54:49

相关问题