2016-02-02 38 views
0

我有一個UserControl,我想添加一個Viewbox。該視圖框位於我的項目中的其他xaml文件中。我嘗試了很多(像ResourceDictionaries的東西)......但失敗了。這是我的小例子:將外部Viewbox導入到UserControl

用戶控件:

<UserControl ......> 
    <Grid> 
    <!--Here I want the Viewbox (MyPicture.xaml)--> 
    </Grid> 
</UserControl> 

MyPicture.xaml

<Viewbox Width="16" Height="16" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> 
    <Rectangle ... /> 
</Viewbox> 

希望有人能幫助我。

+0

「MyPicture.xaml」文件中沒有其他內容?這個想法背後的基本原理是什麼? – romanoza

+0

沒有別的。我只想把這個ViewBox保存在一個外部文件中。 – Fruchtzwerg

+0

所以你不能使用'ResourceDictionary'也不能控制/用戶控制?這真的很有趣,你爲什麼決定使用這種方法。 – romanoza

回答

1

據我所知,您需要重新使用視圖框,如果是這樣,請嘗試下一個解決方案作爲您的研究的起點。

XAML代碼:

<Window x:Class="ResourceDictionaryProblemHelpAttempt.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition></RowDefinition> 
     <RowDefinition></RowDefinition> 
     <RowDefinition></RowDefinition> 
    </Grid.RowDefinitions> 
    <ContentControl Grid.Row="0" ContentTemplate="{StaticResource DataTemplateWithViewBox}"></ContentControl> 
    <ContentControl Grid.Row="1" ContentTemplate="{StaticResource DataTemplateWithViewBox}"></ContentControl> 
    <ContentControl Grid.Row="2" ContentTemplate="{StaticResource DataTemplateWithViewBox}"></ContentControl> 
</Grid> 

資源字典碼

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
<DataTemplate x:Key="DataTemplateWithViewBox"> 
    <Viewbox Width="16" Height="16"> 
     <Rectangle Width="16" Height="16" Fill="Tomato" Stroke="Black" StrokeThickness="1"></Rectangle> 
    </Viewbox> 
</DataTemplate> 

的App.xaml代碼

<Application x:Class="ResourceDictionaryProblemHelpAttempt.App" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     StartupUri="MainWindow.xaml"> 
<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="ViewPortContainingResourceDictionary.xaml"></ResourceDictionary> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 

問候。