<Style x:Key="trumpetBtn" TargetType="{x:Type quJiao:ButtonWithIcon}">
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="IsHitTestVisible" Value="True"/>
<Setter Property="Width" Value="64"/>
<Setter Property="Height" Value="64"/>
<Setter Property="IconWidth" Value="64"/>
<Setter Property="IconHeight" Value="64"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type quJiao:ButtonWithIcon}">
<Grid Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
<Image x:Name="img" Source="{TemplateBinding IconPath}" Width="{TemplateBinding IconWidth}" Height="{TemplateBinding IconHeight}"/>
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding IsPlaying}" Value="True">
<Setter Property="Source" TargetName="img" Value="{Binding Path=SecondIconPath,RelativeSource={RelativeSource AncestorType={x:Type quJiao:ButtonWithIcon}}}"/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Value="{Binding Path=SecondIconPath,RelativeSource={RelativeSource AncestorType={x:Type quJiao:ButtonWithIcon}}}"
如果要在绑定中设置参数,写法如下:
<DataTrigger Binding="{Binding RelativeSource={RelativeSource self},Path=IconPath,Converter={StaticResource BoolConvertObjectIfNull}}" Value="True">
<Setter TargetName="icon" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="spacing" Property="Visibility" Value="Collapsed"/>
</DataTrigger>
Binding="{Binding RelativeSource={RelativeSource self},Path=IconPath}"
converter部分是转换器,用来判断IconPath是否为空的
还有一种情况是在style中的resources中的属性,需要绑定到父级,写法如下
<ControlTemplate.Resources>
<Style TargetType="ContentPresenter">
<Setter Property="Content" Value="{Binding Path=SelectionBoxItem,RelativeSource={RelativeSource TemplatedParent}}"/>
<Setter Property="ContentTemplate" Value="{Binding Path=SelectionBoxItemTemplate,RelativeSource={RelativeSource TemplatedParent}}"/>
<Setter Property="ContentTemplateSelector" Value="{Binding Path=ItemTemplateSelector,RelativeSource={RelativeSource TemplatedParent}}"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
</ControlTemplate.Resources>
Value="{Binding Path=SelectionBoxItem,RelativeSource={RelativeSource TemplatedParent}}"
还有一种是绑定到控件属性,看图即可: