簡體   English   中英

XAML / WPF焦點文本塊

[英]XAML/WPF focus textblock

文本塊可以在WPF中聚焦嗎? 如果當前是重點,我想更改文本塊的背景顏色,但是我想在XAML中進行更改。 這就是我現在所擁有的。 它是Stackpanel中的一堆文本框。 我可以使XAML定位到非焦點或基本狀態,但是當我嘗試添加觸發器時,焦點不會改變背景。 代碼如下:

<Style x:Key="QueueListTextBlocks" TargetType="TextBlock">
            <Setter Property="Background" Value="#027802"></Setter>
            <Setter Property="Foreground" Value="White"></Setter>
            <Setter Property="Padding" Value="10,5"></Setter>
            <Setter Property="Margin" Value="5,2,5,0"></Setter>
            <Setter Property="FontSize" Value="14"></Setter>
            <Setter Property="Focusable" Value="true"/>
            <Setter Property="Cursor" Value="Hand"></Setter>  
            <!-- Trigger-->
            <Style.Triggers>
 <!--Does not pick up a IsFucused State--Alternative?-->

                <Trigger Property="IsFocused" Value="True">
                    <Setter Property="Background" Value="Blue"></Setter>
                    <Setter Property="FontSize" Value="18"></Setter>
                    <Setter Property="Foreground" Value="Orange"></Setter>
                </Trigger>

            </Style.Triggers>
            <!--<Setter Property="Background" Value="White" />-->
        </Style>

我嘗試了您的風格,效果很好。 我的窗口中的TextBlocks只需按TAB鍵即可更改其外觀。 我正在使用.NET 4.0框架。

這是我窗口的XAML:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <Style TargetType="TextBlock" x:Key="TextBlockStyle">
            <Setter Property="Background" Value="#027802"></Setter>
            <Setter Property="Foreground" Value="White"></Setter>
            <Setter Property="Padding" Value="10,5"></Setter>
            <Setter Property="Margin" Value="5,2,5,0"></Setter>
            <Setter Property="FontSize" Value="14"></Setter>
            <Setter Property="Focusable" Value="true"/>
            <Setter Property="Cursor" Value="Hand"></Setter>
            <!-- Trigger-->
            <Style.Triggers>
                <!--Does not pick up a IsFucused State-Alternative?-->

                <Trigger Property="IsFocused" Value="True">
                    <Setter Property="Background" Value="Blue"></Setter>
                    <Setter Property="FontSize" Value="18"></Setter>
                    <Setter Property="Foreground" Value="Orange"></Setter>
                </Trigger>

            </Style.Triggers>
            <!--<Setter Property="Background" Value="White" />-->
        </Style>
    </Window.Resources>

    <StackPanel Orientation="Vertical">
        <TextBlock Text="One" Style="{StaticResource TextBlockStyle}" />
        <TextBlock Text="Two" Style="{StaticResource TextBlockStyle}" />
        <TextBlock Text="Three" Style="{StaticResource TextBlockStyle}" />
        <TextBlock Text="Four" Style="{StaticResource TextBlockStyle}" />
        <TextBlock Text="Five" Style="{StaticResource TextBlockStyle}" />
    </StackPanel>
</Window>

希望對您有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM