wpf怎么改变button里面字体的颜色,就是content的颜色?

wpf怎么改变button里面字体的颜色,就是content的颜色?,第1张

如果是直接在按钮上改变,那么就是设置Foreground就是了,如果修改了模板或样式,并且显示文本的控件并没有绑定到按钮的Foreground属性上,那么就设置对应显示按钮内容控件的颜色

希望对你有帮助,有疑问请追问或是Hi

TreeView treeView1=new TreeView () //创建treeview实例

TreeViewItem item1 = new TreeViewItem()//创建treeview子项实例

item1.Header = "123"//设置文字内容

item1.Foreground = new SolidColorBrush(Colors.AliceBlue) //用固态画刷填充前景色

treeView1.Items.Add(item1)//集合中添加子项

望采纳

WPF一般是通过Style里的触发器来改变这些的,给你个完整的TextBox的Style,你再好好看下WPF教程里讲Style的部分

<Style TargetType="{x:Type TextBox}">

        <Setter Property="KeyboardNavigation.TabNavigation" Value="None" />

        <Setter Property="FocusVisualStyle" Value="{x:Null}" />

        <Setter Property="AllowDrop" Value="true" />

        <Setter Property="Background" >

            <Setter.Value>

                <SolidColorBrush Color="{DynamicResource WhiteColor}"/>

            </Setter.Value>

        </Setter>

        <Setter Property="HorizontalContentAlignment" Value="Stretch" />

        <Setter Property="VerticalContentAlignment" Value="Stretch" />

        <Setter Property="FontFamily" Value="Trebuchet MS" />

        <Setter Property="FontSize" Value="12" />

        <Setter Property="Padding" Value="4" />

        <Setter Property="BorderThickness" Value="1" />

        <Setter Property="BorderBrush" Value="{StaticResource TextControlBorderBrush}" />

<Setter Property="Template" Value="{DynamicResource TextBoxTemplate}" />

    </Style>

        

        <ControlTemplate x:Key="TextBoxTemplate" TargetType="{x:Type TextBox}">

                    <ControlTemplate.Resources>

                        <Storyboard x:Key="HoverOn">

                            <DoubleAnimation Duration="00:00:00.1000000" Storyboard.TargetName="Over" Storyboard.TargetProperty="Opacity" To="1" />

                            <DoubleAnimation Duration="00:00:00.1000000" Storyboard.TargetName="Over_Border" Storyboard.TargetProperty="Opacity" To="1" />

                        </Storyboard>

                        <Storyboard x:Key="HoverOff">

                            <DoubleAnimation Duration="00:00:00.4000000" Storyboard.TargetName="Over" Storyboard.TargetProperty="Opacity" To="0" />

                            <DoubleAnimation Duration="00:00:00.4000000" Storyboard.TargetName="Over_Border" Storyboard.TargetProperty="Opacity" To="0" />

                        </Storyboard>

                        <Storyboard x:Key="FocusedOn">

                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="(UIElement.Opacity)">

                                <SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="1" />

                            </DoubleAnimationUsingKeyFrames>

                        </Storyboard>

                        <Storyboard x:Key="FocusedOff">

                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="(UIElement.Opacity)">

                                <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="0" />

                            </DoubleAnimationUsingKeyFrames>

                        </Storyboard>

                        <Storyboard x:Key="DisabledOn">

                            <ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="DisabledVisualElement" Storyboard.TargetProperty="(UIElement.Visibility)">

                                <DiscreteObjectKeyFrame KeyTime="00:00:00.1000000" Value="{x:Static Visibility.Visible}" />

                            </ObjectAnimationUsingKeyFrames>

                        </Storyboard>

<Storyboard x:Key="DisabledOff">

                            <ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="DisabledVisualElement" Storyboard.TargetProperty="(UIElement.Visibility)">

                                <DiscreteObjectKeyFrame KeyTime="00:00:00.1000000" Value="{x:Static Visibility.Collapsed}" />

                            </ObjectAnimationUsingKeyFrames>

                        </Storyboard>

                    </ControlTemplate.Resources>

                    <Grid>

                        <Border x:Name="BorderBase" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="2.75" />

                        <Border x:Name="Over" BorderBrush="{DynamicResource FocusBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="2.75" Opacity="0" />

                        <Border x:Name="Over_Border" Margin="-1" BorderBrush="{DynamicResource MouseOverHighlightBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="3.75" IsHitTestVisible="False" Opacity="0" />

                        <ScrollViewer Margin="0" x:Name="PART_ContentHost" Foreground="{DynamicResource OutsideFontColor}" />

                        <Border x:Name="DisabledVisualElement" Background="#A5FFFFFF" BorderBrush="#59C0C0C0" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="2.75" IsHitTestVisible="False" Visibility="Collapsed" />

                        <Border x:Name="ReadOnlyVisualElement" Background="#66FFFFFF" CornerRadius="2.75" Visibility="Collapsed" />

                        <Border x:Name="FocusVisualElement" BorderBrush="#FFB1703C" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="2.75" IsHitTestVisible="False" Opacity="0" />

                    </Grid>

                    <ControlTemplate.Triggers>

                        <Trigger Property="IsFocused" Value="True">

                            <Trigger.ExitActions>

                                <BeginStoryboard Storyboard="{StaticResource FocusedOff}" x:Name="FocusedOff_BeginStoryboard" />

                            </Trigger.ExitActions>

                            <Trigger.EnterActions>

                                <BeginStoryboard Storyboard="{StaticResource FocusedOn}" x:Name="FocusedOn_BeginStoryboard" />

                            </Trigger.EnterActions>

                        </Trigger>

                        <MultiTrigger>

                            <MultiTrigger.Conditions>

                                <Condition Property="IsMouseOver" Value="True" />

                                <Condition Property="IsFocused" Value="False" />

                            </MultiTrigger.Conditions>

                            <MultiTrigger.ExitActions>

                                <BeginStoryboard x:Name="HoverOff_BeginStoryboard" Storyboard="{StaticResource HoverOff}" />

                            </MultiTrigger.ExitActions>

                            <MultiTrigger.EnterActions>

                                <BeginStoryboard Storyboard="{StaticResource HoverOn}" />

                            </MultiTrigger.EnterActions>

                        </MultiTrigger>

                        <Trigger Property="IsEnabled" Value="False">

                            <Trigger.EnterActions>

                                <BeginStoryboard Storyboard="{StaticResource DisabledOn}" x:Name="DisabledOn_BeginStoryboard" />

                            </Trigger.EnterActions>

                            <Setter Property="Foreground" Value="{DynamicResource DisabledForegroundBrush}" />

                        </Trigger>

<Trigger Property="IsEnabled" Value="True">

<Trigger.EnterActions>

                                <BeginStoryboard Storyboard="{StaticResource DisabledOff}"  />

                            </Trigger.EnterActions>

<Setter Property="Foreground" Value="#FF000000" />

</Trigger>

                    </ControlTemplate.Triggers>

                </ControlTemplate>


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11080990.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-13
下一篇 2023-05-13

发表评论

登录后才能评论

评论列表(0条)

保存