【问题标题】:Import external Viewbox to UserControl将外部 Viewbox 导入到 UserControl
【发布时间】:2016-05-11 17:32:05
【问题描述】:

我有一个要在其中添加 Viewbox 的 UserControl。 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>

希望有人可以帮助我。

【问题讨论】:

  • MyPicture.xaml 文件中没有其他内容?这个想法背后的基本原理是什么?
  • 没有别的。我只想将此 ViewBox 保存在外部文件中。
  • 所以你不能使用ResourceDictionary也不能控制/用户控制?你为什么决定使用这种方法真的很有趣。
  • 我的计划是在不同的文件中拆分不同的视图框。因此更容易更改它们并在其他应用程序中重用。

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


【解决方案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>

问候。

【讨论】:

    【解决方案2】:

    在资源字典代码中,我不想使用 (code) ,而是只想调用 viewbox 文件。特别是因为我有 2000 行代码和 30 个不同的视图框文件。例如,它会是这样的:

    <ResourceDictionary 
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <DataTemplate x:Key="DataTemplateWithViewBox">
        Get file information of viewbox1.xaml
    </DataTemplate>
    
    <DataTemplate x:Key="DataTemplateWithViewBox2">
        Get file information of viewbox2.xaml
    </DataTemplate>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多