2014-04-15 2 views
2

내 wpf 창에 아래 버튼과 함께 툴바가 있습니다. 제 질문은 템플릿에 대한 구조가 동일하기 때문에 버튼을 템플릿으로 공유 할 수있는 것 같습니다. 내용이지만 이미지 소스와 TextBlock 텍스트가 다르므로 모든 버튼에 대해 중복 된 코드를 제거하는 방법은 무엇입니까? 아니면 이렇게하려면 사용자 지정 컨트롤을 정의해야합니까?ToolBar에있는 버튼에 대해 중복 코드를 제거하는 방법

<ToolBar Name="CommonToolbar"> 
    <Button Name="DownloadButton" Margin="5,0,5,0" Width="Auto" Command="{Binding Path=DownloadCmd}" ToolTip="{x:Static resx:GeneralRes.DownloadToolbarTooltip}" ToolTipService.ShowOnDisabled="True"> 
     <Grid> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="16"/> 
       <RowDefinition /> 
      </Grid.RowDefinitions> 
      <Image Grid.Row="0" Source="Resources/Download.png" /> 
      <TextBlock Grid.Row="1" Text="{x:Static resx:GeneralRes.DownloadToolbarCaption}" /> 
     </Grid> 
    </Button> 

    <Button Name="UploadButton" Margin="5,0,5,0" Width="Auto" Command="{Binding Path=UploadCmd}" ToolTip="{x:Static resx:GeneralRes.UploadToolbarTooltip}" ToolTipService.ShowOnDisabled="True"> 
     <Grid> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="16"/> 
       <RowDefinition/> 
      </Grid.RowDefinitions> 
      <Image Grid.Row="0" Source="Resources/Upload.png"/> 
      <TextBlock Grid.Row="1" Text="{x:Static resx:GeneralRes.UploadToolbarCaption}"/> 
     </Grid> 
    </Button> 

    <Button Name="ManualButton" Margin="5,0,5,0" Width="Auto" ToolTip="{x:Static resx:GeneralRes.ManualToolbarTooltip}" > 
     <Grid> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="16"/> 
       <RowDefinition/> 
      </Grid.RowDefinitions> 
      <Image Grid.Row="0" Source="Resources/help manual.png"/> 
      <TextBlock Grid.Row="1" Text="{x:Static resx:GeneralRes.ManualToolbarCaption}"/> 
     </Grid> 
    </Button> 

</ToolBar> 

답변

1

, 잘 들으 작동이

<Window.Resources> 
    <DataTemplate x:Key="ButtonContenttemplate" DataType="{x:Type Button}"> 
     <Grid> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="16"/> 
       <RowDefinition /> 
      </Grid.RowDefinitions> 
      <Image Grid.Row="0" Source="{Binding Path=Tag,RelativeSource={RelativeSource AncestorType=Button}}" /> 
      <TextBlock Grid.Row="1" Text="{Binding Path=Content,RelativeSource={RelativeSource AncestorType=Button}}" /> 
     </Grid> 
    </DataTemplate> 
</Window.Resources> 

<Button Name="DownloadButton" Tag="catalogscreen.png" Content="HEllo world" ContentTemplate="{StaticResource ButtonContenttemplate}" Margin="5,0,5,0" ToolTipService.ShowOnDisabled="True"/> 
+0

시도 –

관련 문제