Wpf从资源中重用UI元素

在我的界面上有几个选项卡,每个选项卡中都有下面的元素:

<StackPanel Orientation="Horizontal">
     <Button Content="&lt;" Command="{Binding MoveToPreviousCommand}" /> 
     <Button Content="&gt;" Command="{Binding MoveToNextCommand}" />
</StackPanel>

有没有办法避免复制此代码到XAML中每个选项卡中,能不能在资源中定义一次上面的元素,每个选项卡都指向UI资源呢?答案是肯定的。

<Window.Resources>       
    <StackPanel x:Key="Content" Orientation="Horizontal">
        <Button Content="&lt;" Command="{Binding MoveToPreviousCommand}" /> 
        <Button Content="&gt;" Command="{Binding MoveToNextCommand}" />
    </StackPanel>

    <DataTemplate x:Key="template1">
        <StackPanel  Orientation="Horizontal">
            <Button Content="&lt;" Command="{Binding MoveToPreviousCommand}" /> 
            <Button Content="&gt;" Command="{Binding MoveToNextCommand}" />
        </StackPanel>
    </DataTemplate>
</Window.Resources>

<StackPanel>
    <ContentControl Content="{StaticResource Content}"></ContentControl>
    <ContentControl Name="contCtrl" ContentTemplate="{StaticResource template1}" Content="This is the content of the content control."/>
</StackPanel>
posted on 2014-07-11 23:22  明月几时有25  阅读(560)  评论(0编辑  收藏  举报