简体   繁体   中英

Can you access the gradient colour created in XAML in c# wpf

Similar questions may exists, but none of them seems to be helpful. So I'll try to explain a more specific case, and see if anyone can help me.

I have an application that has a label with a gradient background. I created the gradient background using XAML, my program changes the background fine, but I want the background to change back to "normal" after the user clicks the reset button. How can I do this as still new to wpf? The code in XAML "LinearGradientBrush x:Key="headerBackground" is what I want to access in code behind

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:syncfusion="http://schemas.syncfusion.com/wpf" x:Class="ItemWrapPanel.MainWindow"
    Title="MainWindow" Height="350" Width="600">
<Window.Resources>
    **<LinearGradientBrush x:Key="headerBackground" StartPoint="0,0" EndPoint="0,1">
        <GradientStop Color="#FFEFEEEE" Offset="0"/>
        <GradientStop Color="#E7E7E7E7" Offset="1"/>
    </LinearGradientBrush>**
    <LinearGradientBrush x:Key="selectedHeaderBackground" StartPoint="0,0" EndPoint="0,1">
        <GradientStop Color="#F6CD1D" Offset="0"/>
        <GradientStop Color="#EBA32A" Offset="1"/>
    </LinearGradientBrush>
</Window.Resources>
<Grid x:Name="MainGrid">
    <Grid.RowDefinitions>
        <RowDefinition x:Name="Row1"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition x:Name="Col1"/>
        <ColumnDefinition x:Name="Col2"/>
        <ColumnDefinition x:Name="Col3"/>
    </Grid.ColumnDefinitions>
    <Label x:Name="Label1"
        Content="Rock Properties"
        Grid.Row="0"
        Grid.Column="0"
        MouseDoubleClick="Label1_MouseDoubleClick"
        Background="{DynamicResource headerBackground}"
        VerticalAlignment="Top"/>
    <Label x:Name="Label2"
        Content="Contacts"
        Grid.Column="1"
        Grid.Row="0"
        Background="{DynamicResource headerBackground}"
        MouseDoubleClick="Label2_MouseDoubleClick" 
        Margin="0,0,0,282"
        VerticalAlignment="Top"/>
    <Label x:Name="Label3"
        Content="Fluid Properties"
        Grid.Column="2"
        Background="{DynamicResource headerBackground}"
        Grid.Row="0"
        MouseDoubleClick="Label3_MouseDoubleClick" 
        Margin="0,0,0,282"
        VerticalAlignment="Top"/>
    <Button x:Name="Resetbtn" Content="Reset" HorizontalAlignment="Left" Margin="0,70,0,0" VerticalAlignment="Top" Width="75" Click="Reset_Click" Background="{DynamicResource selectedHeaderBackground}"/>
    <Button Name="CloseRock" Content="X" HorizontalAlignment="Left" Margin="173,4,0,0" VerticalAlignment="Top" Width="24" Grid.Column="0"/>
    <Button Name="CloseContacts" Content="X" HorizontalAlignment="Left" Margin="174,2,0,0" VerticalAlignment="Top" Width="24" Grid.Column="1"/>
    <Button Name="CloseFluid" Content="X" HorizontalAlignment="Left" Margin="173,2,0,0" VerticalAlignment="Top" Width="24" Grid.Column="2"/>
</Grid>

C#

 private ColumnDefinition SelectedColumn;
    private Color selectedHeaderBackground = (Color)ColorConverter.ConvertFromString("#EBA32A");

    public MainWindow()
    {
        InitializeComponent();
    }

    private void Label1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        SelectedColumn = Col1;
        SetCol1Header();
        SetColumnWidth();
        SetColumnWidthCol1();
    }

    private void Label2_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        SelectedColumn = Col2;
        SetCol2Header();
        SetColumnWidth();
        SetColumnWidthCol2();
    }

    private void Label3_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        SelectedColumn = Col3;
        SetCol3Header();
        SetColumnWidth();
        SetColumnWidthCol3();
    }

    public void SetColumnWidth()
    {
        SelectedColumn.Width = new GridLength(1, GridUnitType.Star);
    }

    private void Reset_Click(object sender, RoutedEventArgs e)
    {
        ResetHeaderBackground();
        ResetColumnWidth();
        ResetColumns();
    }

    private void ResetColumns()
    {
        Label1.Visibility = System.Windows.Visibility.Visible;
    }

    private void ResetColumnWidth()
    {
        Col1.Width = new GridLength(1, GridUnitType.Star);
        Col2.Width = new GridLength(1, GridUnitType.Star);
        Col3.Width = new GridLength(1, GridUnitType.Star);
    }

    private void ResetHeaderBackground()
    {
        //Label3.Background = new SolidColorBrush(HeaderBackground);
        //Label2.Background = new SolidColorBrush(HeaderBackground);
        //Label1.Background = new SolidColorBrush(HeaderBackground);
    }

    private void MinimiseRock_Click(object sender, RoutedEventArgs e)
    {
        MinimiseWindow();
    }

    private void MinimiseContacts_Click(object sender, RoutedEventArgs e)
    {
        MinimiseWindow();
    }

    private void MinimiseFluid_Click(object sender, RoutedEventArgs e)
    {
        MinimiseWindow();
    }

    private void MinimiseWindow()
    {
        this.WindowState = WindowState.Minimized;
    }

    private void SetCol1Header()
    {
        Label1.Background = new SolidColorBrush(selectedHeaderBackground);
    }

    private void SetCol2Header()
    {
        Label2.Background = new SolidColorBrush(selectedHeaderBackground);
    }

    private void SetCol3Header()
    {
        Label3.Background = new SolidColorBrush(selectedHeaderBackground);
    }

    private void SetColumnWidthCol1()
    {
        Col2.Width = new GridLength(150);
        Col3.Width = new GridLength(150);
    }

    private void SetColumnWidthCol2()
    {
        Col1.Width = new GridLength(150);
        Col3.Width = new GridLength(150);
    }

    private void SetColumnWidthCol3()
    {
        Col1.Width = new GridLength(150);
        Col2.Width = new GridLength(150);
    }

}

You can access the Resources of your Window by invoking FindResource providing the key you have set for your Brush (and eventually cast it to LinearGradientBrush).

this.Label1.Background = (LinearGradientBrush)this.FindResource("headerBackground")

cheeers.

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