2016-08-25 3 views
1

사용자 정의 버튼 StyleXAML으로 작성되었습니다. 그것은 이미지와 텍스트가있는 버튼입니다. 그러나 Image은 맞춤 설정할 수 있어야합니다. 디자이너에서 Source 속성을 변경해야합니다.XAML에서 리소스 템플릿의 자식 값을 변경하는 방법?

내 코드 :

<Window.Resources> 
    <ResourceDictionary> 
    <Style x:Key="SSbutton" TargetType="{x:Type Button}"> 
      <Setter Property="Cursor" Value="Hand"/> 
      <Setter Property="Background" Value="Green"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="Button"> 
        <Viewbox Stretch="Uniform"> 
          <Border Background="{TemplateBinding Background}" Width="{TemplateBinding Width}" 
           Height="{TemplateBinding Height}"> 
           <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Center"> 
            <!--I want to change this Source property--> 
            <Image Source="img/desktop.png" Width="30" HorizontalAlignment="Left" /> 
            <TextBlock Margin="3,0,0,0" HorizontalAlignment="Right" VerticalAlignment="Center" 
              Text="{TemplateBinding Content}" FontSize="{TemplateBinding FontSize}"/> 
           </StackPanel> 
          </Border> 
         </Viewbox> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
    </ResourceDictionary> 
</Window.Resources> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="25"/> 
     <RowDefinition /> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="90" /> 
     <ColumnDefinition /> 
    </Grid.ColumnDefinitions> 
    <Border Grid.Column="0" Grid.Row="1" Background="LightGreen"> 
    <StackPanel > 
     <Button Style="{StaticResource SSbutton}" Width="90" Height="30" Content="Desktop" FontSize="13" 
       Foreground="White"/> 
    </StackPanel> 
    </Border> 
</Grid> 

내가 어떻게 할 수 있습니까?

답변

1

손쉬운 멋쟁이로 임의의 템플릿 바인딩을 사용하여 속성에 피기 백 Tag 속성;

<Style x:Key="SSbutton" TargetType="{x:Type Button}"> 
      <Setter Property="Cursor" Value="Hand"/> 
      <Setter Property="Background" Value="Green"/> 
      <!-- Set a default --> 
      <Setter Property="Tag" Value="img/desktop.png"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="Button"> 
        <Viewbox Stretch="Uniform"> 
          <Border Background="{TemplateBinding Background}" Width="{TemplateBinding Width}" 
           Height="{TemplateBinding Height}"> 
           <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Center"> 
            <!--I want to change this Source property--> 
            <Image Source="{Binding Path=Tag, RelativeSource={RelativeSource TemplatedParent}}" Width="30" HorizontalAlignment="Left" /> 
            <TextBlock Margin="3,0,0,0" HorizontalAlignment="Right" VerticalAlignment="Center" 
              Text="{TemplateBinding Content}" FontSize="{TemplateBinding FontSize}"/> 
           </StackPanel> 
          </Border> 
         </Viewbox> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

그런 다음 인스턴스에서;

<Button Style="{StaticResource SSbutton}" 
     Tag="Some/Other/Image.png" 
     Width="90" Height="30" 
     Content="Desktop" 
     FontSize="13" Foreground="White"/> 

호프가 도움이 되었으면 좋겠다.

편집 : OP의 설명에 따라 wpf의 templatebinding에 대한 경로 고려 사항을 반영하여 업데이트되었습니다.

+0

시도해 보셨습니까? 나는 그것을 작동하게 만들 수 없었다. 나는 복사 - 붙여 넣기를 만들었지 만 그것은 이미지를 보여주지 못했습니다. 내가 뭔가 잘못하고 있는거야? –

+0

http://stackoverflow.com/questions/4638741/template-binding-in-control-template에서 관련 질문을 발견하고'{TemplateBinding Tag}'코드를 {Binding Path = Tag, RelativeSource = {RelativeSource TemplatedParent}}'. 그래서 그것은 잘 작동합니다. 이 방법을 사용하거나 귀하의 질문을 업데이트하면 동의 할 것입니다. ** ** ** 그러나이 방법은 런타임에만 작동합니다. 이미지는 디자인 타임이 아닌 런타임에만 표시됩니다. –

+1

아, 죄송합니다. x : 서식을 복사/붙여 넣을 때 입력에주의를 기울이지 않아서 질문에 WPF 태그가 없으므로 범용으로 간주됩니다. 이미지의 빌드 동작을 resource로 설정하고 리터럴 파일 경로 대신 pack uri를 사용하면 디자인 타임에 작동해야합니다. –

관련 문제