What is the best way to reuse blocks of XAML?

后端 未结 1 1908
天命终不由人
天命终不由人 2021-02-04 08:28

I\'ve got many user controls like this:

PageManageCustomers.xaml.cs:

public partial class PageManageCustomers : BasePage
{
 ...
}


        
相关标签:
1条回答
  • 2021-02-04 09:22

    Use resource dictionaries - add a MyDictionary.xaml file to your project, setting its Build Action to "Page":

    <ResourceDictionary
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <DataTemplate x:Key="manageAreaCellTemplate">
            <StackPanel Orientation="Horizontal">
                <TextBlock Style="{DynamicResource ManageLinkStyle}"
                  Tag="{Binding Id}" Text="Delete" MouseDown="System_Delete_Click"/>
                <TextBlock Text=" "/>
                <TextBlock Style="{DynamicResource ManageLinkStyle}"
                  Tag="{Binding Id}" Text="Edit" MouseDown="System_Edit_Click"/>
            </StackPanel>
        </DataTemplate>
    </ResourceDictionary>
    

    Then in other XAML files you refer to it with:

    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="MyDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
        ... some other local resources ...
    </ResourceDictionary>
    

    and refer to your resource as Template={StaticResource manageAreaCellTemplate}.

    0 讨论(0)
提交回复
热议问题