WpfリソースからのUI要素の再利用

5489 ワード

私のインタフェースにはいくつかのタブがあります.各タブには次の要素があります.
<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>