我写了以下风格:
<Style TargetType="Button" x:Key="ImageButton">
<Setter Property="Width" Value="{Binding ActualHeight, RelativeSource={RelativeSource Self}}"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="Margin" Value="5 0 5 0"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="Transparent"/>
</Style>
<Style TargetType="Button" x:Key="AddButton" BasedOn="{StaticResource ImageButton}">
<Setter Property="Content">
<Setter.Value>
<Image Source="/MyProject;component/Images/Icons/Add.png"/>
</Setter.Value>
</Setter>
<Setter Property="Command" Value="{Binding AddCommand}"/>
</Style>
<Style TargetType="Button" x:Key="DeleteButton" BasedOn="{StaticResource ImageButton}">
<Setter Property="Content">
<Setter.Value>
<Image Source="/MyProject;component/Images/Icons/Delete.png"/>
</Setter.Value>
</Setter>
<Setter Property="Command" Value="{Binding DeleteCommand}"/>
</Style>
我通过以下方式申请:
<Button Style="{StaticResource DeleteButton}" Grid.Row="0" Grid.Column="0"/>
<Button Style="{StaticResource AddButton}" Grid.Row="0" Grid.Column="1"/>
所有这些都发生在带有不同标签的TabControl中。再次从某个选项卡更改为此选项卡后,图像将消失。当我从其他选项卡更改为此选项卡时,图像不会消失。 有没有人有任何想法为什么这个出现?
更新 - 解决方案:
<DataTemplate x:Key="AddTemplate">
<Image Source="/MyProject;component/Images/Icons/Add.png"/>
</DataTemplate>
<Style TargetType="Button" x:Key="AddButton" BasedOn="{StaticResource ImageButton}">
<Setter Property="ContentTemplate" Value="{StaticResource AddTemplate}"/>
<Setter Property="Command" Value="{Binding AddCommand}"/>
</Style>