开发者

How to set that all Lable foreground is White in Window?

开发者 https://www.devze.com 2023-01-30 05:06 出处:网络
Follow method is invalid,I dont want to set color by ergodic. How to set by style: Style styleLable = new System.Windows.Style(typeof(Label)开发者_如何学JAVA);

Follow method is invalid,I dont want to set color by ergodic. How to set by style:

Style styleLable = new System.Windows.Style(typeof(Label)开发者_如何学JAVA);
styleLable.Setters.Add(new Setter(Label.ForegroundProperty, "White"));
this.Resources.Add("", styleLable);
this.UpdateLayout();


You can try something like this:

<Style TargetType="{x:Type Label}" x:Key="LabelBase">
<Setter Property="Foreground" Value="White"/>
    <Setter Property="Template">
    <Setter.Value>

    <ControlTemplate TargetType="{x:Type Label}">
    <Grid>
    <Border x:Name="Rectangle_MouseOver"BorderThickness="1" Background="{x:Null}" CornerRadius="4" Height="Auto">
        <ContentPresenter x:Name="Content" Margin="4,0,4,0"/>
    </Border>
    </Grid>
    <ControlTemplate.Triggers>
        <Trigger/>
    </ControlTemplate.Triggers>
    </ControlTemplate>
    </Setter.Value>
</Setter>


I got the same problem, then i got the answer. here is the answer.

  1. add this tag to your .xaml page.

    "<Grid Name="myGrid1">"
    
  2. add that control to your grid and set Foreground using Brushes Class.

    Label myLabel = new Label();
    myLabel.Content = "New Element"; 
    myGrid1.Children.Add(newLabel);
    newLabel.Foreground = Brushes.Green;
    


There are two ways to do this:

I recommend using TextBlock instead of Label. It will be a lot easier:

<StackPanel TextBlock.Foreground="White">
    <TextBlock Text="Some Text" />
    <TextBlock Text="Some Text" />
    <TextBlock Text="Some Text" />
    <TextBlock Text="Some Text" />
    <TextBlock Text="Some Text" />
</StackPanel>

With Label, you can do it like following:

<StackPanel>
    <StackPanel.Resources>
        <Style TargetType="Label">
            <Setter Property="Foreground"
                    Value="White" />
        </Style>
    </StackPanel.Resources>
    <Label Content="Some Text" />
    <Label Content="Some Text" />
    <Label Content="Some Text" />
    <Label Content="Some Text" />
    <Label Content="Some Text" />
</StackPanel>
0

精彩评论

暂无评论...
验证码 换一张
取 消