加载位于MainWindow.xaml

时间:2017-06-14 12:40:24

标签: c# wpf xaml

目标:创建一个XAML模板,我可以重复使用并加载到我的主视图中。这可能吗?如果是这样的话?我已经阅读了有关ResourceDictionary的内容并提出了一些建议,但我不确定从哪里继续。

这是我的资源的XAML(保持非常愚蠢和简单):

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <StackPanel x:Key="myUnneccesaryButtonList">
        <Button Content="1"></Button>
        <Button Content="2"></Button>
        <Button Content="3"></Button>
    </StackPanel>

</ResourceDictionary>

这里是我的MainWindow XAML,我想使用上面的模板并加载它:

<Window x:Class="Sample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"

        Title="Sample" WindowState="Maximized">

    <StackPanel x:Name="wrapper" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">

    </StackPanel>
</Window>

编辑:这是我的MainWindow,但Window.Resource声明不起作用:

<Window x:Class="Sample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"

        Title="Sample" WindowState="Maximized">

    <Window.Resources>
        <ResourceDictionary Source="MyDictionary.xaml" >
    </Window.Resources>

    <StackPanel x:Name="wrapper" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">

    </StackPanel>
</Window>

2 个答案:

答案 0 :(得分:3)

myUnneccesaryButtonList不是模板,而是实际的StackPanel实例。

如果您在x:Shared中将false属性设置为ResourceDictionary

<StackPanel x:Key="myUnneccesaryButtonList" x:Shared="False">
    <Button Content="1"></Button>
    <Button Content="2"></Button>
    <Button Content="3"></Button>
</StackPanel>

..您可以使用ContentControl在窗口中显示它:

<ContentControl Content="{StaticResource myUnneccesaryButtonList}" />

您可能想要做的是创建一个自定义StackPanel类,但始终添加Buttons

public class CustomStackPanel : System.Windows.Controls.StackPanel
{
    public CustomStackPanel()
    {
        Children.Add(new Button() { Content = "1" });
        Children.Add(new Button() { Content = "2" });
        Children.Add(new Button() { Content = "3" });
    }
}

<强>用法:

<local:CustomStackPanel x:Name="wrapper" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>

答案 1 :(得分:2)

如果你想要一个XML模板,那么你应该创建一个模板

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <DataTemplate x:Key="myUnneccesaryButtonList">
        <StackPanel >
            <Button Content="1"></Button>
            <Button Content="2"></Button>
            <Button Content="3"></Button>
        </StackPanel>
    </DataTemplate>
</ResourceDictionary>

然后你可以定义一个控件来托管它

<ContentControl ContentTemplate="{StaticResource myUnneccesaryButtonList}" />

<ItemsControl ItemTemplate="{StaticResource myUnneccesaryButtonList}" />

请记住将字典添加到资源

<Window.Resources>
    <ResourceDictionary Source="YourDictionary.xaml" />
</Window.Resources>

或将其合并到

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="YourDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>