2017-06-14 72 views
0

目標:創建一個XAML模板,我可以將其重新加載並加載到我的主視圖中。這可能嗎?如果是這樣如何?我已閱讀ResourceDictionary並提出了一些建議,但我不知道從哪裏繼續。加載位於MainWindow.xaml內的單獨文件中的資源

這是我的資源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> 

這裏我的主窗口的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> 
+0

你缺少結尾的斜線:'' – mm8

回答

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

將字典添加到資源中發生在我的MainWindow權利?我不斷收到一個錯誤,指出Resource屬性找不到ResourceDictionary – Asperger

+0

它可能發生在應用程序窗口中的一個控件,只要它從你想要使用它的樹上移開即可 – MikeT

+0

記住Source的值必須是您的xaml文件的URI爲 – MikeT

3

myUnneccesaryButtonList不是模板而是實際的StackPanel實例。

如果您在ResourceDictionary設置其x:Shared屬性false

<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"/> 
+0

不要以爲這些按鈕就是例子,所以不要以爲自定義的stackpanel是一個好主意。一般來說,您將很難在代碼中爲這些「模板」創建整個可視化樹。 – Evk

+0

這就是控制模板的用途,但這是另一回事。 – mm8

+0

非常有趣。這仍然在ResourceDictionary中?因爲你省略了標籤,所以我問。 – Asperger