简体   繁体   English

加载位于MainWindow.xaml中单独文件中的资源

[英]Load resources located in seperate files inside the MainWindow.xaml

Goal: Creating an XAML template which I can reuse and load into my main view. 目标:创建一个XAML模板,可以重复使用并加载到主视图中。 Is this possible? 这可能吗? If so how? 如果可以,怎么办? Ive read about the ResourceDictionary and came up with something but im not sure where to continue from there. 我已经阅读了ResourceDictionary并提出了一些建议,但是我不确定从哪里继续。

This is the XAML with my resource (kept very dumb and simple): 这是带有我的资源的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>

Here my MainWindow XAML where I want to use the above template and load it: 在这里,我要使用上面的模板并加载它的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>

Edit: Here is my MainWindow but the Window.Resource declaration doesnt work: 编辑:这是我的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>

myUnneccesaryButtonList is not a template but an actual StackPanel instance. myUnneccesaryButtonList不是模板,而是实际的StackPanel实例。

If you set its x:Shared attribute to false in the ResourceDictionary : 如果在ResourceDictionary中将其x:Shared属性设置为false

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

..you could use a ContentControl to display it in the window: ..您可以使用ContentControl在窗口中显示它:

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

What you probably want to do is to create a custom StackPanel class that always adds the Buttons though: 您可能想要做的是创建一个自定义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" });
    }
}

Usage: 用法:

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

if you want a XML Template then you should create a template 如果您想要一个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>

then you could define a control to host it 然后您可以定义一个控件来托管它

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

or 要么

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

Remember add the dictionary into the Resources 记住将字典添加到参考资料中

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

or to merge it in 或合并到

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM