1
사용자 정의 버튼 Style
이 XAML
으로 작성되었습니다. 그것은 이미지와 텍스트가있는 버튼입니다. 그러나 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>
내가 어떻게 할 수 있습니까?
시도해 보셨습니까? 나는 그것을 작동하게 만들 수 없었다. 나는 복사 - 붙여 넣기를 만들었지 만 그것은 이미지를 보여주지 못했습니다. 내가 뭔가 잘못하고 있는거야? –
http://stackoverflow.com/questions/4638741/template-binding-in-control-template에서 관련 질문을 발견하고'{TemplateBinding Tag}'코드를 {Binding Path = Tag, RelativeSource = {RelativeSource TemplatedParent}}'. 그래서 그것은 잘 작동합니다. 이 방법을 사용하거나 귀하의 질문을 업데이트하면 동의 할 것입니다. ** ** ** 그러나이 방법은 런타임에만 작동합니다. 이미지는 디자인 타임이 아닌 런타임에만 표시됩니다. –
아, 죄송합니다. x : 서식을 복사/붙여 넣을 때 입력에주의를 기울이지 않아서 질문에 WPF 태그가 없으므로 범용으로 간주됩니다. 이미지의 빌드 동작을 resource로 설정하고 리터럴 파일 경로 대신 pack uri를 사용하면 디자인 타임에 작동해야합니다. –