문제

표준이 있습니다 Button Style Applied My Application은 텍스트 콘텐츠를 흰색에서 검정으로 변경하는 마우스 오버 스타일을 지정하는 내 응용 프로그램을 흉내냅니다. 이를 달성하기 위해 버튼 템플릿 수정을 시도하고 ContentPresenter 그리고 그것을 a로 교체합니다 TextBlock. 그러나 이것은 다음과 같은 혼합 된 컨텐츠를 가질 수 없음을 의미합니다.

<Button
    <Button.Content>
        <StackPanel Orientation="Horizontal">
            <Path Margin="3" Width="12.375" Height="9.70833" Canvas.Left="0" Canvas.Top="0"
                  Stretch="Fill" Fill="#FF115485"
                  Data="F1 M 18.8333,7.08333L 16.7083,4.91667L 10.5,10.5417L 8.52083,8.6875L 6.45833,10.4792L 10.4792,14.625L 18.8333,7.08333 Z "/>
            <TextBlock Margin="3">Hello</TextBlock>
        </StackPanel>
    </Button.Content>
</Button>

상황을 개선하기 위해 나는 ContentPresenter 다시 들어가서 내 (텍스트 만) 버튼에 지정된 ContentTemplate 이와 같이:

<Button IsDefault="True" Content="Text only Button"
        ContentTemplate="{StaticResource TextButtonTemplate}"
        Command="{Binding ...}" 
        CommandParameter="{...}" />

그리고 a Template 아래와 같이 정의됩니다. 이 템플릿은 만 작동합니다 Button텍스트 컨텐츠와 함께, 다른 템플릿이 혼합 내용에 필요에 따라 정의되어 있습니다.

<DataTemplate x:Key="TextButtonTemplate">
    <TextBlock Text="{TemplateBinding Content}"
               Foreground="{Binding Path=Foreground,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Button}}" />
</DataTemplate>

사용자 정의 템플릿을 정의하지 않고 원하는 마우스 오버 스타일을 유지함으로써 더 개선 할 수있는 방법이 있는지 아는 사람이 있습니까?

도움이 되었습니까?

해결책

콘텐츠가 명시 적으로 자체적으로 설정되지 않는다고 가정하면 다음과 같이 달성 할 수 있습니다.

<Style x:Key="MyButtonStyle" TargetType="{x:Type Button}">
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="TextBlock.Foreground" Value="Red" />
        </Trigger>
    </Style.Triggers>
</Style>

TextBlock.foreground는 상속 된 속성이기 때문에 작동합니다. 버튼 내용의 컨트롤이 명시 적으로 자체 전경을 설정하지 않는 한 작동합니다. 그들이 자신의 색상을 설정하면 모든 베팅이 꺼져 있습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top