如何从ResourceDictionary重用网格定义?

时间:2014-10-15 14:14:04

标签: wpf xaml resourcedictionary

我正在尝试重新使用在ResourceDictionary下定义的Grid(guiLayout.xaml)

guiLayout.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">    
<Grid x:Key="guiLayout">
    <Grid.RowDefinitions>
        <RowDefinition Height="10"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
</Grid>

我还有 MainWindow.xaml

<Window x:Class="WpfApplication3.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>
    <!-- I want to use ResourceDictionary definitions here-->
</Grid>

是否可以在guiLayout.xml中使用Mainwindow.xaml中的网格定义?

修改

项目看起来像这样,

enter image description here

1 个答案:

答案 0 :(得分:2)

您可以像这样在XAML中设置Content的{​​{1}}:

Window

或使用元素语法:

<Window x:Class="WpfApplication3.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"
        Content="{StaticResource guiLayout}">
</Window>

请注意,如果<Window x:Class="WpfApplication3.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"> <Window.Content> <StaticResource ResourceKey="guiLayout"/> </Window.Content> </Window> 未保存为资源x:Shared,则Grid仅可用于1个窗口。要将其用于多个窗口,您必须为Grid添加x:Shared="false",如下所示:

Grid

更新:尝试使用此代码导入资源:

App.xaml

<Grid x:Key="guiLayout" x:Shared="false">
   <!-- ... -->
</Grid>