将外部Viewbox导入UserControl

时间:2016-02-02 18:47:30

标签: c# .net wpf xaml user-controls

我有一个UserControl,我想添加一个Viewbox。 Viewbox位于我项目中的另一个xaml文件中。我尝试了很多(比如ResourceDictionaries)......但是失败了。这是我的最小例子:

用户控件:

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

希望有人能帮助我。

1 个答案:

答案 0 :(得分: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>

问候。