By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,994 Members | 2,073 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,994 IT Pros & Developers. It's quick & easy.

wpf

P: 2
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.Resources["CurvedButton"] as Style;

Error 2
An object reference is required for the non-static field, method, or property 'System.Windows.FrameworkElement.Resources.get'

Anybody can provide any form of assistance?


Expand|Select|Wrap|Line Numbers
  1.  
  2. using System;
  3. using System.IO;
  4. using System.Net;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Media;
  9. using System.Windows.Media.Animation;
  10. using System.Windows.Navigation;
  11.  
  12.  
  13. namespace ListBoxMenuSample
  14. {
  15.     /// <summary>
  16.     /// Interaction logic for dynamicPage.xaml
  17.     /// </summary>
  18.     public partial class dynamicPage
  19.     {
  20.         public dynamicPage()
  21.         {
  22.                this.InitializeComponent();
  23.             populateButtons();
  24.         }
  25.  
  26.         public void populateButtons()
  27.         {
  28.             int xPos;
  29.             int yPos;
  30.  
  31.             Random ranNum = new Random();
  32.  
  33.             for (int i = 0; i < 50; i++)
  34.             {
  35.                 Button foo = new Button();
  36.                Style buttonStyle = Window.Resources["CurvedButton"] as Style;
  37.  
  38.                 int sizeValue = ranNum.Next(50);
  39.  
  40.                 foo.Width = sizeValue;
  41.                 foo.Height = sizeValue;
  42.  
  43.                 xPos = ranNum.Next(300);
  44.                 yPos = ranNum.Next(200);
  45.  
  46.                 foo.HorizontalAlignment = HorizontalAlignment.Left;
  47.                 foo.VerticalAlignment = VerticalAlignment.Top;
  48.                 foo.Margin = new Thickness(xPos, yPos, 0, 0);
  49.  
  50.                foo.Style = buttonStyle;
  51.                 foo.Name = "button" + i;
  52.  
  53.                 foo.Click += new RoutedEventHandler(buttonClick);
  54.  
  55.                 LayoutRoot.Children.Add(foo);
  56.            }
  57.         }
  58.  
  59.         private void buttonClick(object sender, EventArgs e)
  60.         {
  61.             Button clicked = (Button) sender;
  62.             MessageBox.Show("Button's name is: " + clicked.Name);
  63.         }
  64.     }
  65.  }
  66.  
  67.  
  68.  
  69.  


Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. <Window x:Class="ListBoxMenuSample.dynamicPage"
  4.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  5.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  6.         Title="dynamicPage" Height="300" Width="300">
  7.  
  8.     <Window.Resources>
  9.         <Style x:Key="CurvedButton" BasedOn="{x:Null}" TargetType="{x:Type Button}">
  10.             <Setter Property="Template">
  11.                 <Setter.Value>
  12.                     <ControlTemplate TargetType="{x:Type Button}">
  13.                         <ControlTemplate.Resources>
  14.                             <Storyboard x:Key="OnMouseMove1">
  15.                                 <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
  16.                                     <SplineColorKeyFrame KeyTime="00:00:00" Value="#FFFFFFFF"/>
  17.                                     <SplineColorKeyFrame KeyTime="00:00:00.3000000" Value="#7CE1DBDB"/>
  18.                                 </ColorAnimationUsingKeyFrames>
  19.                                 <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)">
  20.                                     <SplineDoubleKeyFrame KeyTime="00:00:00" Value="1"/>
  21.                                     <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="1.66"/>
  22.                                 </DoubleAnimationUsingKeyFrames>
  23.                                 <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)">
  24.                                     <SplineDoubleKeyFrame KeyTime="00:00:00" Value="1"/>
  25.                                     <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="1.66"/>
  26.                                 </DoubleAnimationUsingKeyFrames>
  27.                             </Storyboard>
  28.                             <Storyboard x:Key="OnMouseLeave1">
  29.                                 <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)">
  30.                                     <SplineDoubleKeyFrame KeyTime="00:00:00.8000000" Value="1.78"/>
  31.                                     <SplineDoubleKeyFrame KeyTime="00:00:01" Value="1"/>
  32.                                 </DoubleAnimationUsingKeyFrames>
  33.                                 <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)">
  34.                                     <SplineDoubleKeyFrame KeyTime="00:00:00.8000000" Value="1.78"/>
  35.                                     <SplineDoubleKeyFrame KeyTime="00:00:01" Value="1"/>
  36.                                 </DoubleAnimationUsingKeyFrames>
  37.                             </Storyboard>
  38.                             <Storyboard x:Key="OnClick1">
  39.                                 <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
  40.                                     <SplineColorKeyFrame KeyTime="00:00:00.2000000" Value="#FFFFFFFF"/>
  41.                                     <SplineColorKeyFrame KeyTime="00:00:00.3000000" Value="#BFA0D1E2"/>
  42.                                 </ColorAnimationUsingKeyFrames>
  43.                             </Storyboard>
  44.                         </ControlTemplate.Resources>
  45.                         <Grid>
  46.                             <Rectangle RenderTransformOrigin="0.5,0.5" Fill="#3FFFFFFF" Stroke="{x:Null}" RadiusX="11" RadiusY="11" x:Name="rectangle">
  47.                                 <Rectangle.RenderTransform>
  48.                                     <TransformGroup>
  49.                                         <ScaleTransform ScaleX="1" ScaleY="1"/>
  50.                                         <SkewTransform AngleX="0" AngleY="0"/>
  51.                                         <RotateTransform Angle="0"/>
  52.                                         <TranslateTransform X="0" Y="0"/>
  53.                                     </TransformGroup>
  54.                                 </Rectangle.RenderTransform>
  55.                             </Rectangle>
  56.                             <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True"/>
  57.                         </Grid>
  58.                         <ControlTemplate.Triggers>
  59.                             <EventTrigger RoutedEvent="ButtonBase.Click">
  60.                                 <BeginStoryboard x:Name="OnClick1_BeginStoryboard" Storyboard="{StaticResource OnClick1}"/>
  61.                             </EventTrigger>
  62.                             <EventTrigger RoutedEvent="Mouse.MouseLeave">
  63.                                 <BeginStoryboard x:Name="OnMouseLeave1_BeginStoryboard" Storyboard="{StaticResource OnMouseLeave1}"/>
  64.                             </EventTrigger>
  65.                             <EventTrigger RoutedEvent="FrameworkElement.Loaded"/>
  66.                             <EventTrigger RoutedEvent="Mouse.MouseEnter">
  67.                                 <BeginStoryboard x:Name="OnMouseMove1_BeginStoryboard" Storyboard="{StaticResource OnMouseMove1}"/>
  68.                             </EventTrigger>
  69.                             <Trigger Property="IsFocused" Value="True"/>
  70.                             <Trigger Property="IsDefaulted" Value="True"/>
  71.                             <Trigger Property="IsMouseOver" Value="True"/>
  72.                             <Trigger Property="IsPressed" Value="True"/>
  73.                             <Trigger Property="IsEnabled" Value="False"/>
  74.                         </ControlTemplate.Triggers>
  75.                     </ControlTemplate>
  76.                 </Setter.Value>
  77.             </Setter>
  78.             <Setter Property="Background">
  79.                 <Setter.Value>
  80.                     <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
  81.                         <GradientStop Color="#FFF3F3F3" Offset="0"/>
  82.                         <GradientStop Color="#FFEBEBEB" Offset="0.5"/>
  83.                         <GradientStop Color="#FFDDDDDD" Offset="0.5"/>
  84.                         <GradientStop Color="#E1CDCDCD" Offset="1"/>
  85.                     </LinearGradientBrush>
  86.                 </Setter.Value>
  87.             </Setter>
  88.         </Style>
  89.     </Window.Resources>
  90.     <Window.Triggers>
  91.         <EventTrigger RoutedEvent="FrameworkElement.Loaded"/>
  92.     </Window.Triggers>
  93.     <Window.Background>
  94.         <LinearGradientBrush EndPoint="0.484,0.543" StartPoint="0.478,0.009">
  95.             <GradientStop Color="#FF2A3641" Offset="1"/>
  96.             <GradientStop Color="#FF7B8EA1" Offset="0"/>
  97.         </LinearGradientBrush>
  98.     </Window.Background>
  99.  
  100.     <Grid x:Name="LayoutRoot">
  101.         <Grid.ColumnDefinitions>
  102.             <ColumnDefinition Width="*"/>
  103.         </Grid.ColumnDefinitions>
  104.     </Grid>
  105.  
  106. </Window>
  107.  
  108.  
  109.  
Oct 13 '10 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.