wpf

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hhenryyho
    New Member
    • Oct 2010
    • 2

    #1

    wpf

    hi all,
    I went around and did some research about static methods but it does not seems to apply to my case.

    The problem lies with the below line :

    Style buttonStyle = Window.Resource s["CurvedButt on"] as Style;

    Error 2
    An object reference is required for the non-static field, method, or property 'System.Windows .FrameworkEleme nt.Resources.ge t'

    Anybody can provide any form of assistance?


    Code:
    using System;
    using System.IO;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Navigation;
    
    
    namespace ListBoxMenuSample
    {
        /// <summary>
        /// Interaction logic for dynamicPage.xaml
        /// </summary>
        public partial class dynamicPage
        {
            public dynamicPage()
            {
               	this.InitializeComponent();
                populateButtons();
            }
    
            public void populateButtons()
            {
                int xPos;
                int yPos;
    
                Random ranNum = new Random();
    
                for (int i = 0; i < 50; i++)
                {
                    Button foo = new Button();
                   Style buttonStyle = Window.Resources["CurvedButton"] as Style;
    
                    int sizeValue = ranNum.Next(50);
    
                    foo.Width = sizeValue;
                    foo.Height = sizeValue;
    
                    xPos = ranNum.Next(300);
                    yPos = ranNum.Next(200);
    
                    foo.HorizontalAlignment = HorizontalAlignment.Left;
                    foo.VerticalAlignment = VerticalAlignment.Top;
                    foo.Margin = new Thickness(xPos, yPos, 0, 0);
    
                   foo.Style = buttonStyle;
                    foo.Name = "button" + i;
    
                    foo.Click += new RoutedEventHandler(buttonClick);
    
                    LayoutRoot.Children.Add(foo);
               }
            }
    
            private void buttonClick(object sender, EventArgs e)
            {
                Button clicked = (Button) sender;
                MessageBox.Show("Button's name is: " + clicked.Name);
            }
    	}
     }


    Code:
    
    <Window x:Class="ListBoxMenuSample.dynamicPage"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="dynamicPage" Height="300" Width="300">
    
        <Window.Resources>
            <Style x:Key="CurvedButton" BasedOn="{x:Null}" TargetType="{x:Type Button}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <ControlTemplate.Resources>
                                <Storyboard x:Key="OnMouseMove1">
                                    <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
                                        <SplineColorKeyFrame KeyTime="00:00:00" Value="#FFFFFFFF"/>
                                        <SplineColorKeyFrame KeyTime="00:00:00.3000000" Value="#7CE1DBDB"/>
                                    </ColorAnimationUsingKeyFrames>
                                    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)">
                                        <SplineDoubleKeyFrame KeyTime="00:00:00" Value="1"/>
                                        <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="1.66"/>
                                    </DoubleAnimationUsingKeyFrames>
                                    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)">
                                        <SplineDoubleKeyFrame KeyTime="00:00:00" Value="1"/>
                                        <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="1.66"/>
                                    </DoubleAnimationUsingKeyFrames>
                                </Storyboard>
                                <Storyboard x:Key="OnMouseLeave1">
                                    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)">
                                        <SplineDoubleKeyFrame KeyTime="00:00:00.8000000" Value="1.78"/>
                                        <SplineDoubleKeyFrame KeyTime="00:00:01" Value="1"/>
                                    </DoubleAnimationUsingKeyFrames>
                                    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)">
                                        <SplineDoubleKeyFrame KeyTime="00:00:00.8000000" Value="1.78"/>
                                        <SplineDoubleKeyFrame KeyTime="00:00:01" Value="1"/>
                                    </DoubleAnimationUsingKeyFrames>
                                </Storyboard>
                                <Storyboard x:Key="OnClick1">
                                    <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
                                        <SplineColorKeyFrame KeyTime="00:00:00.2000000" Value="#FFFFFFFF"/>
                                        <SplineColorKeyFrame KeyTime="00:00:00.3000000" Value="#BFA0D1E2"/>
                                    </ColorAnimationUsingKeyFrames>
                                </Storyboard>
                            </ControlTemplate.Resources>
                            <Grid>
                                <Rectangle RenderTransformOrigin="0.5,0.5" Fill="#3FFFFFFF" Stroke="{x:Null}" RadiusX="11" RadiusY="11" x:Name="rectangle">
                                    <Rectangle.RenderTransform>
                                        <TransformGroup>
                                            <ScaleTransform ScaleX="1" ScaleY="1"/>
                                            <SkewTransform AngleX="0" AngleY="0"/>
                                            <RotateTransform Angle="0"/>
                                            <TranslateTransform X="0" Y="0"/>
                                        </TransformGroup>
                                    </Rectangle.RenderTransform>
                                </Rectangle>
                                <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True"/>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <EventTrigger RoutedEvent="ButtonBase.Click">
                                    <BeginStoryboard x:Name="OnClick1_BeginStoryboard" Storyboard="{StaticResource OnClick1}"/>
                                </EventTrigger>
                                <EventTrigger RoutedEvent="Mouse.MouseLeave">
                                    <BeginStoryboard x:Name="OnMouseLeave1_BeginStoryboard" Storyboard="{StaticResource OnMouseLeave1}"/>
                                </EventTrigger>
                                <EventTrigger RoutedEvent="FrameworkElement.Loaded"/>
                                <EventTrigger RoutedEvent="Mouse.MouseEnter">
                                    <BeginStoryboard x:Name="OnMouseMove1_BeginStoryboard" Storyboard="{StaticResource OnMouseMove1}"/>
                                </EventTrigger>
                                <Trigger Property="IsFocused" Value="True"/>
                                <Trigger Property="IsDefaulted" Value="True"/>
                                <Trigger Property="IsMouseOver" Value="True"/>
                                <Trigger Property="IsPressed" Value="True"/>
                                <Trigger Property="IsEnabled" Value="False"/>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Setter Property="Background">
                    <Setter.Value>
                        <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                            <GradientStop Color="#FFF3F3F3" Offset="0"/>
                            <GradientStop Color="#FFEBEBEB" Offset="0.5"/>
                            <GradientStop Color="#FFDDDDDD" Offset="0.5"/>
                            <GradientStop Color="#E1CDCDCD" Offset="1"/>
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
            </Style>
        </Window.Resources>
        <Window.Triggers>
            <EventTrigger RoutedEvent="FrameworkElement.Loaded"/>
        </Window.Triggers>
        <Window.Background>
            <LinearGradientBrush EndPoint="0.484,0.543" StartPoint="0.478,0.009">
                <GradientStop Color="#FF2A3641" Offset="1"/>
                <GradientStop Color="#FF7B8EA1" Offset="0"/>
            </LinearGradientBrush>
        </Window.Background>
    
        <Grid x:Name="LayoutRoot">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
        </Grid>
    
    </Window>
Working...