2012-12-26 88 views
1

在我的項目,我有XAML文件是這樣的:移動XAML文件的一部分到另一個XAML文件

<Grid Margin="50,0,0,0"> 
    //Huge amount of code goes here 
</Grid> 

這是很難通過所有的代碼Grid而設計。我可以將所有代碼移動到單獨的XAML文件文件中,並且在此Grid內容中我將調用該XAML文件?

<Grid Margin="50,0,0,0"> 
    //call xaml file here 
</Grid> 

回答

2

您應該定義一個UserControl;假設「的代碼巨大的量」是某事像這樣:

<Border Name="yourBorder"> 
     //Other Xamls 
</Border> 

現在你創建一個新的用戶控件的把這個邊界在它

<UserControl x:Class="WpfApplication.UserControl1" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300"> 
<Border Name="yourBorder"> 
     //Other Xamls 
</Border> 
</UserControl> 

您可以在其他Xamls使用UserControl1。您應該將xmlns:wp="clr-namespace:WpfApplication"添加到您的Xaml中。例如,如果你想在一個窗口中使用它:

<Window x:Class="WpfApplication.Window2" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:wp="clr-namespace:WpfApplicationUpper"  //This is what I mentioned 
    Title="Window2" Height="300" Width="300"> 
<StackPanel> 
    <wp:UserControl1 /> // You call it using this format 
</SatckPanel> 
相關問題