双击标签时如何更改标签的背景色?

时间:2019-05-09 18:49:17

标签: c# wpf

在运行应用程序时双击标签时尝试在c#中更改标签的颜色时遇到问题。我已经尝试过各种方法,但是找不到解决方案。伙计们,我知道这可能是一个新手问题,但是如果您能帮助我,我将不胜感激。谢谢!这是WPF:

<Border Padding="10">
    <StackPanel>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="3*"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <TextBlock x:Name="BestScore" Margin="0 0 30 0" Height="20" Grid.Column="0" Background="#CDCDCD"></TextBlock>
            <Button x:Name="PlayAgainButton" Click="PlayAgainButton_Click" Height="50" Grid.Column="1" Background="#F1EB5F" Content="Play Again"></Button>
            <TextBlock x:Name="BestScoreFromFile" Margin="30 0 0 0" Height="20" Grid.Column="2" Background="#CDCDCD"></TextBlock>
        </Grid>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Label x:Name="Label1" Height="100" MouseDoubleClick="Label1_MouseDoubleClick" Grid.Column="0" Margin="0 60 20 0" Background="Aqua"></Label>
            <Label x:Name="Label2" Height="100" MouseDoubleClick="Label2_MouseDoubleClick" Grid.Column="1" Margin="0 60 0 0" Background="Aqua"></Label>
            <Label x:Name="Label3" Height="100" MouseDoubleClick="Label3_MouseDoubleClick" Grid.Column="2" Margin="20 60 0 0" Background="Aqua"></Label>
        </Grid>

    </StackPanel>
</Border>

还有C#:

 private void Label1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        Label1.Background =Color.Red;
    }

1 个答案:

答案 0 :(得分:1)

尝试使用

Label1.Background =Brushes.Red;