2012-03-28 1 views
0

Ribbon이있는 WPF 응용 프로그램을 비트 맵 대신 단추 등의 이미지에 벡터 그래픽으로 변환해야합니다. 지금까지는 리본 메뉴 항목의 왼쪽에 표시되는 그래픽을 설정하기 위해 ImageSource 유형의 RibbonApplicationMenu.ImageSource 속성을 사용하는 RibbonApplicationMenu 항목을 제외하고는이 작업을 수행했습니다. XAML에서 정의한 벡터 그래픽을 리본 메뉴 항목의 이미지로 사용하려면 어떻게해야합니까?

내 벡터 그래픽

XAML에 다음과 같이 정의된다 : 나는 Button.Template

내가 알아낼 수 없습니다 수를 설정할 때

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

다음은 그러나, 제대로 렌더링 , RibbonApplicationMenu 컨트롤에 표시되는 이미지에 사용하는 방법에 대해 설명합니다. RibbonApplicationMenu.Template 속성을 설정하는 것만으로는 작동하지 않습니다. 전체 컨트롤이 채워져 기능이 중단 된 것 같습니다.

나는로 사용할 RenderTargetBitmapVisualBrush를 사용 후 렌더링을 시도한 .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

두 가지를 수행합니다 : 은 첫째로 당신이 그것에게 폭과 높이 (자세한 내용은 the upvoted answer here 참조)를 할당해야합니다의 ControlTemplateCanvas 설정 명시 적 크기도 없다. 다른 한 가지는 ArrangeMeasureViewbox으로되어있어 필요한 크기로 가정해야합니다. 에

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

희망이 작업을 얻을 것이다

그래서 뭔가에 벡터 그래픽에 대한 XAML을 변경.

+0

죄송합니다. 위의 코드에 더 자세히 설명해 주셨으면합니다. 나는 캔버스 용으로 설정된 크기를 가지고있다 (그리고 나는 그 질문에 편집했다.). 나는 Measure and Arrange asap을 시도 할 것이다. – Pysul

+0

'.Measure'와'.Arrange'를 사용하면 리본에 나타날 것이 있습니다. 나는 그것이 내가 필요한 것을 정확하게 모르겠다. 그러나 그것은 효과가 있었다. 제대로 정렬되지는 않았지만, 아마도 그걸 가지고 놀 수 있습니다. – Pysul