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>

转载于:https://www.cnblogs.com/moonlight-zjb/p/3838691.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值