Asim Zaidi
Asim Zaidi

Reputation: 28294

how to access the grids on silverlight

I have a button called test button an when I click on it I want to show a grid whose visbility is set to 0

I created a mouse button event (below) but my Grid (testGrid) is not available in the project.

private void testButton(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {       
           testGrid.Opacity = 1;                                                                              
        }

it highlights testGrid red

new to SL so not sure whats going on here

** EDIT **

xml for the Grid

    <Grid x:Name="testGrid" HorizontalAlignment="Left" Width="150" Margin="950,-77,0,0" Height="77" VerticalAlignment="Top" Opacity="0">

    </Grid> 



<Image x:Name="testButton" HorizontalAlignment="Right" Margin="0,1,180,3"  Stretch="Fill" Width="53" Height="49"  Cursor="Hand" Opacity="0.8" >

Upvotes: 0

Views: 112

Answers (2)

jcvegan
jcvegan

Reputation: 3170

try this: On XAML

<Grid x:Name="testGrid" HorizontalAlignment="Left" Width="150" Margin="950,-77,0,0" Height="77" VerticalAlignment="Top" Opacity="0">
</Grid>
<Image x:Name="testButton" HorizontalAlignment="Right" Margin="0,1,180,3"  Stretch="Fill" Width="53" Height="49"  Cursor="Hand" Opacity="0.8" MouseLeftButtonDown="testButton" />

on the cs

private void testButton(object sender, System.Windows.Input.MouseButtonEventArgs e){       
       testGrid.Visibility = Visibility.Visible;
}

Upvotes: 1

Chris
Chris

Reputation: 3162

The reason that your grid might not be accessibly in the .cs file is if you have changed the class name in the .cs file, but not in the Xaml directive at the top of your .xaml file.

If these two mis match, visual studio won't be able to link up the two files and so you wouldn't be able to see the grid control in the code behind.

Other items with your code to consider:

Though Opacity will work an alternative I use more often is:

This will show the grid.

testGrid.Visibility = Visibility.Visible;

This will hide the grid.

testGrid.Visibility = Visibility.Collapsed;

Also, Your "Button" is an Image tag and not a button. It is an image with the same name as the method you are trying to call. You will either need to change your Image to allow for an on click event or change it to a button, something like

<Button Click="TestBUtton" Text="MyButton" />

And I'd enter that in the Xaml directly so that when you type in the click event handler it auto generates the method in the code behind for you.

Upvotes: 2

Related Questions