WPF TextBlock渐变

时间:2014-06-02 08:20:00

标签: c# wpf

如何将其从XAML转换为c#

<TextBlock x:Name="Cell0" FontSize="72" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="50">
            <TextBlock.Background>
                <RadialGradientBrush>
                    <GradientStop Color="Cyan" Offset="0"/>
                    <GradientStop Color="White" Offset="1"/>
                </RadialGradientBrush>
            </TextBlock.Background>1</TextBlock>

我已经尝试过这样的

RadialGradientBrush radialGradientBrush = new RadialGradientBrush();
            radialGradientBrush.GradientOrigin = new Point(5, 5);
            radialGradientBrush.Center = new Point(5, 5);
            radialGradientBrush.RadiusX = 5;
            radialGradientBrush.RadiusY = 5;
            radialGradientBrush.GradientStops.Add(new GradientStop(Colors.Cyan, 0.0));
            radialGradientBrush.GradientStops.Add(new GradientStop(Colors.White, 1));
Cell0.Background = radialGradientBrush

然而,这不起作用,然后我尝试将半径5替换为10并显示青色,但不是渐变

3 个答案:

答案 0 :(得分:0)

仅初始化您在XAML中执行的相同元素,因此在您的情况下不要设置GradientOriginCenterRadiusX / RadiusY并将其简化为

RadialGradientBrush radialGradientBrush = new RadialGradientBrush();
radialGradientBrush.GradientStops.Add(new GradientStop(Colors.Cyan, 0.0));
radialGradientBrush.GradientStops.Add(new GradientStop(Colors.White, 1));
Cell0.Background = radialGradientBrush;

答案 1 :(得分:0)

试试这个

   RadialGradientBrush myRadialGradientBrush = new RadialGradientBrush();
    myRadialGradientBrush.GradientOrigin = new Point(0.5, 0.5);
    myRadialGradientBrush.Center = new Point(0.5, 0.5);
    myRadialGradientBrush.RadiusX = 0.5;
    myRadialGradientBrush.RadiusY = 0.5;
    myRadialGradientBrush.GradientStops.Add(
                    new GradientStop(Colors.Cyan, 0.0));
    myRadialGradientBrush.GradientStops.Add(
                    new GradientStop(Colors.White, 1.0));

    cell0.Fill = myRadialGradientBrush;

答案 2 :(得分:0)

<UserControl.Resources>
    <LinearGradientBrush x:Key="GridLabelColor" StartPoint="0.25,0" EndPoint="0.65,1" Opacity=".5">
        <GradientStop Color="AliceBlue" Offset="0"/>
        <GradientStop Color="Silver" Offset=".35"/>
        <GradientStop Color="Navy" Offset="0.85"/>
    </LinearGradientBrush>
</UserControl.Resources>