locked
LinearGradientBrush in app.xaml RRS feed

  • Question

  • hi,

    I have a button with

    <Button.Background>
                    <LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
                        <GradientStop Color="#FF0A4E82" Offset="0.333"/>
                        <GradientStop Color="White"/>
                    </LinearGradientBrush>
                </Button.Background>

    I want to have all my buttons in the application have the same gradient. How do i put it in app.xaml?

    Thanks

    Saturday, November 25, 2017 12:13 PM

Answers

  • Here is a simple example of what you need.

    Application.XAML

    <Application x:Class="Application"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        StartupUri="MainWindow.xaml">
        <Application.Resources>
            <LinearGradientBrush x:Key="LGB" EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
                <GradientStop Color="#FF0A4E82" Offset="0.333"/>
                <GradientStop Color="White"/>
            </LinearGradientBrush>
    
            <Style TargetType="{x:Type Button }">
                <Setter Property="Background" Value="{StaticResource LGB}"></Setter>
                <Setter Property="Width" Value="100"></Setter>
                <Setter Property="Height" Value="30"></Setter>
                <Setter Property="Foreground" Value="White"></Setter>
                <Setter Property="Margin" Value="0,0,20,0"></Setter>
            </Style>
        </Application.Resources>
    </Application>
    

    Window using the above code:

    <Window x:Class="MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApplication1"
            mc:Ignorable="d"
            Title="MainWindow" Height="350" Width="525">
        <WrapPanel >
            <Button>One</Button>
            <Button>Two</Button>
            <Button>Three</Button>
            <Button>Four</Button>
            <Button>Five</Button>
        </WrapPanel>
    </Window>
    


    Lloyd Sheen

    • Marked as answer by Barbi Rio Saturday, November 25, 2017 6:44 PM
    Saturday, November 25, 2017 5:37 PM

All replies

  • Here is a simple example of what you need.

    Application.XAML

    <Application x:Class="Application"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        StartupUri="MainWindow.xaml">
        <Application.Resources>
            <LinearGradientBrush x:Key="LGB" EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
                <GradientStop Color="#FF0A4E82" Offset="0.333"/>
                <GradientStop Color="White"/>
            </LinearGradientBrush>
    
            <Style TargetType="{x:Type Button }">
                <Setter Property="Background" Value="{StaticResource LGB}"></Setter>
                <Setter Property="Width" Value="100"></Setter>
                <Setter Property="Height" Value="30"></Setter>
                <Setter Property="Foreground" Value="White"></Setter>
                <Setter Property="Margin" Value="0,0,20,0"></Setter>
            </Style>
        </Application.Resources>
    </Application>
    

    Window using the above code:

    <Window x:Class="MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApplication1"
            mc:Ignorable="d"
            Title="MainWindow" Height="350" Width="525">
        <WrapPanel >
            <Button>One</Button>
            <Button>Two</Button>
            <Button>Three</Button>
            <Button>Four</Button>
            <Button>Five</Button>
        </WrapPanel>
    </Window>
    


    Lloyd Sheen

    • Marked as answer by Barbi Rio Saturday, November 25, 2017 6:44 PM
    Saturday, November 25, 2017 5:37 PM
  • Thanks Lloyd
    Saturday, November 25, 2017 6:44 PM