【问题标题】:Label foreground in toolbar工具栏中的标签前景
【发布时间】:2021-11-21 17:19:18
【问题描述】:

我的 WPF 窗口中有一个工具栏,如下所示,请帮助我在后面的代码中动态更改标签前景色。 谢谢...

 <DockPanel Height="60" VerticalAlignment="Top">
            <ToolBar Name="MyToolBar" DockPanel.Dock="Top" Height="60" VerticalAlignment="Top" FlowDirection="RightToLeft"
                     FontFamily="Calibri" FontSize="16" FontWeight="Bold" Background="#FFEEF5FD" Foreground="Black"              ScrollViewer.VerticalScrollBarVisibility="Disabled">
               
                <Button Name="BtnSubmit" Style="{DynamicResource TStyle}" Click="BtnSubmit_Click" Tag="New" Height="52" Width="60"
                        IsDefault="True" Margin="0,0,0,1">
                    <StackPanel Margin="0" Height="54"  
                        <Image Source="/Img/Save 04.png" Height="25" Margin="16,0"/>
                        <Label Content="Save" Margin="0,0,0,0" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
                               HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </StackPanel>
                </Button>
                               
                <Button Name="BtnApply" Style="{DynamicResource TStyle}" Click="BtnApply_Click" Tag="New" Height="52"
                        IsDefault="True" Margin="0,1,0,2" Width="85">
                    <StackPanel Margin="0" Height="52" Width="82">
                        <Image Source="/Img/ApplyIcon.png" Height="25" Margin="29,0,26,0"/>
                        <Label Content="Edit" Margin="-15,0,-23,0" HorizontalContentAlignment="Center"                                             VerticalContentAlignment="Center"
                               HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </StackPanel>
                </Button>
               
            </ToolBar>
        </DockPanel>

【问题讨论】:

  • 设置Label的Foreground属性?你真的应该用 TextBlock 替换 Label。
  • 谢谢,我用文本块替换了标签,看起来更好。
  • 但是如何在代码后面动态改变它的前景色。
  • 我使用此代码在后面的代码中更改工具栏背景颜色 (MyToolBar.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#" + bgColor));)
  • 但是文本块的前景色呢?

标签: wpf toolbar foreground


【解决方案1】:

要更改所有TextBlock 元素中的Foreground,您必须定义一个Brush 资源并使用{DynamicResource} 将其分配给所有相关的TextBlock 元素:

MainWindow.xaml

<Window>
  <Window.Resources>
    <SolidColorBrush x:Key="ButtonLabelForegroundBrush" Color="Black" />
  </Window.Resources>

  <DockPanel>
    <ToolBar>
      <Button>
        <TextBlock Foreground="{DynamicResource ButtonLabelForegroundBrush}" />
      </Button>
    </ToolBar>
  </DockPanel>
</Window>

MainWindow.xaml.cs

private void ChangeTextBlockForeground()
{
  var brush = FindResource("ButtonLabelForegroundBrush") as SolidColorBrush;
  brush.Color = Colors.Red;
}

【讨论】:

  • 谢谢,亲爱的朋友。这绝对是我想要的
猜你喜欢
  • 1970-01-01
  • 2011-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-10
  • 1970-01-01
  • 2014-02-22
相关资源
最近更新 更多