1

我有以下内容:

<TextBox HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" FontSize="12" Header="Name" >
    <TextBox.HeaderTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}" Foreground="Red" />
        </DataTemplate>
</TextBox.HeaderTemplate>

有没有办法通过 DataTemplate 或 Xaml 行为来改变前景?

4

1 回答 1

0

您可以使用XamlBehaviors.

在 Xaml 中添加 NugetMicrosoft.Xaml.Behaviors.Uwp.Managed和引用。

 xmlns:Core="using:Microsoft.Xaml.Interactions.Core"
 xmlns:Interactivity="using:Microsoft.Xaml.Interactivity"

处理PointerEnteredPointerExited事件TextBlock

<TextBox HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" FontSize="12" Header="Name" >
    <TextBox.HeaderTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}" Foreground="Red">
                <Interactivity:Interaction.Behaviors>
                    <Core:EventTriggerBehavior EventName="PointerEntered">
                        <Core:ChangePropertyAction PropertyName="Foreground">
                            <Core:ChangePropertyAction.Value>
                                <SolidColorBrush Color="Blue" />
                            </Core:ChangePropertyAction.Value>
                        </Core:ChangePropertyAction>
                    </Core:EventTriggerBehavior>
                    <Core:EventTriggerBehavior EventName="PointerExited">
                        <Core:ChangePropertyAction PropertyName="Foreground">
                            <Core:ChangePropertyAction.Value>
                                <SolidColorBrush Color="Red" />
                            </Core:ChangePropertyAction.Value>
                        </Core:ChangePropertyAction>
                    </Core:EventTriggerBehavior>
                </Interactivity:Interaction.Behaviors>
            </TextBlock>
        </DataTemplate>
    </TextBox.HeaderTemplate>
</TextBox>
于 2021-02-20T14:43:26.747 回答