简体   繁体   中英

WPF Application how to convert grid background xaml to c#?

I have a wpf app that I would like to allow the user to switch the 'Theme', which is simply the grid background.

This is the XAML:

<Grid x:Name="rabGrid">
    <Grid.Background>
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="#FF080D39"/>
            <GradientStop Color="White"/>
            <GradientStop Color="#FF838589" Offset="1"/>
            <GradientStop Color="#FF2E4999" Offset="0.997"/>
        </LinearGradientBrush>
    </Grid.Background>

Here's what I have in c#:

    private void rTheme_Click(object sender, RoutedEventArgs e)
    {

        LinearGradientBrush rabThem = new LinearGradientBrush();
        rabThem.StartPoint = new Point(0.5, 0);
        rabThem.EndPoint = new Point(0.5, 1);

        GradientStop rabThemStop = new GradientStop();
        rabThemStop.Color = Color.FromArgb(100, 8, 13, 57);

        GradientStop rabThemStop1 = new GradientStop();
        rabThemStop1.Color = Colors.White;

        GradientStop rabThemStop2 = new GradientStop();
        rabThemStop2.Color = Color.FromArgb(100, 131, 133, 137);
        rabThemStop2.Offset = 1;

        GradientStop rabThemStop3 = new GradientStop();
        rabThemStop3.Color = Color.FromArgb(100, 46, 73, 153);
        rabThemStop3.Offset = .997;

    }

My idea behind the above code is to setup all the metrics, then somehow fill to the background.

I have also tried variations like this with no joy:

        clasGrid.Background = new LinearGradientBrush();
        clasGrid.StartPoint = new Point(0.5, 0);
        clasGrid.EndPoint = new Point(0.5, 1);

I'm can change the background with something like this:

clasGrid.Background = new LinearGradientBrush(Colors.LightBlue, Colors.SlateBlue, 90);

Implementing some of the syntax from the answer below, I've came up with this:

        LinearGradientBrush rabThem = new LinearGradientBrush();
        rabThem.EndPoint = new Point(0.5, 1);
        rabThem.StartPoint = new Point(0.5, 0);

        GradientStop rabThemStop = new GradientStop();
        rabThemStop.Color = Color.FromArgb(100, 8, 13, 57);

        GradientStop rabThemStop1 = new GradientStop();
        rabThemStop1.Color = Colors.White;

        GradientStop rabThemStop2 = new GradientStop();
        rabThemStop2.Color = Color.FromArgb(100, 131, 133, 137);
        rabThemStop2.Offset = 1;

        GradientStop rabThemStop3 = new GradientStop();
        rabThemStop3.Color = Color.FromArgb(100, 46, 73, 153);
        rabThemStop3.Offset = .997;


        rabThem.GradientStops.Add(rabThemStop);
        rabThem.GradientStops.Add(rabThemStop1);
        rabThem.GradientStops.Add(rabThemStop2);
        rabThem.GradientStops.Add(rabThemStop3);


        clasGrid.Background = rabThem;

While this works, the colors are a lot lighter (as if the gradient is not correct) and am still trying to figure that part out.

You have to set up the gradient each time. You can either make a method for it, or you can make a converter. I'm using a boolean to control which background is used, but you can use whatever value you want. If you choose to make a method, you can base it off of my converter.

   public class OptionOrderBackgroundConverter : IValueConverter
   {
      public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
         // Create a background and return
         bool b = (value != null ? (bool)value : false);
         if (!b)
         {
            LinearGradientBrush lb = new LinearGradientBrush();
            lb.StartPoint = new System.Windows.Point(0, 0);
            lb.EndPoint = new System.Windows.Point(0, 1);
            GradientStop gstop = new GradientStop(ViewModels.QuoteButtonStyle.Instance.QuoteButtonTopBackgroundColor, 0);
            lb.GradientStops.Add(gstop);
            gstop = new GradientStop(ViewModels.QuoteButtonStyle.Instance.QuoteButtonBottomBackgroundColor, 0.9);
            lb.GradientStops.Add(gstop);
            return lb;
         }
         else
         {
            LinearGradientBrush lb = new LinearGradientBrush();
            lb.StartPoint = new System.Windows.Point(0, 0);
            lb.EndPoint = new System.Windows.Point(0, 1);
            GradientStop gstop = new GradientStop(Colors.Orange, 0);
            lb.GradientStops.Add(gstop);
            gstop = new GradientStop(Colors.WhiteSmoke, 0.9);
            lb.GradientStops.Add(gstop);
            return lb;
         }
      }

      public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
         throw new NotImplementedException();
      }
   }

For the converter, I bind it to the control like this:

Background="{Binding Path=IsDealable, FallbackValue=False, Converter={StaticResource OptionOrderBackgroundConverter}}"

If you use a converter and want to be able to pass in 2 colors, you can change it to an IMultiValueConverter .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM