您可以在c#wpf中访问在XAML中创建的渐变颜色

时间:2015-09-15 13:25:31

标签: c# wpf xaml

可能存在类似的问题,但它们似乎都没有帮助。因此,我将尝试解释一个更具体的案例,看看是否有人可以帮助我。

我有一个带有渐变背景标签的应用程序。我使用XAML创建了渐变背景,我的程序改变了背景,但我希望背景改回" normal"用户单击重置按钮后。我怎么能这样做仍然是wpf的新手? XAML中的代码" LinearGradientBrush x:Key =" headerBackground" 是我想要在代码中访问的内容

<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);
    }

}

1 个答案:

答案 0 :(得分:1)

您可以通过调用FindResource来访问您的Window的资源,提供您为Brush设置的键(并最终将其转换为LinearGradientBrush)。

$ sudo ipvsadm -Ln
IP Virtual Server version 1.2.1 (size = 4096)
Prot LocalAddress: Port Scheduler Flags
   -> RemoteAddress: Port Forward Weight ActiveConn InActConn
TCP 172.20.10.5:8090 rr
   -> 127.0.0.1:8090 Route 1 0 0

cheeers。