posts - 32, comments - 52, trackbacks - 0

My Links

News

Archives

Image Galleries

About Me

StackPanel IsEnabled

 Sometimes you need to enable / disable all the controls that are inside a StackPanel or a Grid depending on some conditions… well it seems something pretty straight forward until you realize that these elements does not have available the IsEnabled property …  ouch! What to do? Well… going control by control setting the IsEnabled property is a pain in the neck, encapsulating that elements in an user control can be an elegant solution but doesn’t fit to all scenarios (maybe is just a simple layout, or you don’t have the time to make all that refactoring). Is there any easier solution?...
Today googling into the Silverlight forums found a nice workaround (http://forums.silverlight.net/forums/t/85404.aspx) you can just wrap up your stackpanel or grid control inside a ContentControl
Let’s check how it works with a simple sample: We have the following layout / XAML:
Simple form enabled
        <StackPanel Orientation="Vertical" HorizontalAlignment="Center" Margin="5">       
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="20"/>
                    <RowDefinition Height="20"/>
                </Grid.RowDefinitions>
               
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="70"/>
                    <ColumnDefinition Width="90"/>
                </Grid.ColumnDefinitions>
               
                <TextBlock Text="Fullname" Grid.Row="0" Grid.Column="0" />
                <TextBox Grid.Row="0" Grid.Column="1"/>
                <TextBlock Text="Address" Grid.Row="1" Grid.Column="0"/>
                <TextBox Grid.Row="1" Grid.Column="1"/>
            </Grid>
            <Button Content="Update" Margin="5" Width="100"/>
        </StackPanel>
 
Let’s Wrap the stack panel with the content template and set it’s IsEnabled property to false, gotcha !! we get the desired behavior with just two lines of code :).
 
    <ContentControl IsEnabled="false">
        <StackPanel Orientation="Vertical" HorizontalAlignment="Center" Margin="5">       
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="20"/>
                    <RowDefinition Height="20"/>
                </Grid.RowDefinitions>
               
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="70"/>
                    <ColumnDefinition Width="90"/>
                </Grid.ColumnDefinitions>
               
                <TextBlock Text="Fullname" Grid.Row="0" Grid.Column="0" />
                <TextBox Grid.Row="0" Grid.Column="1"/>
                <TextBlock Text="Address" Grid.Row="1" Grid.Column="0"/>
                <TextBox Grid.Row="1" Grid.Column="1"/>
            </Grid>
            <Button Content="Update" Margin="5" Width="100"/>
        </StackPanel>
    </ContentControl>
 
Form StackPanel IsEnabled False 
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Print | posted on Saturday, September 26, 2009 4:44 PM |

Feedback

Gravatar

# re: StackPanel IsEnabled

Do not forget to set HorizontalContentAlignment="Stretch" and VerticalContentAlignment="Stretch" for the content control, to have the same layout behaviour as before :)

This trick also works for all other properties of the Control class, like FontSize
9/27/2009 10:52 AM | Sebastian
Gravatar

# re: StackPanel IsEnabled

This is exactly the solution that I needed. Thanks! :)
8/16/2010 8:21 PM | SoftwareDeveloper
Gravatar

# Mr

I really enjoy my visit to this site really very nice writing
1/6/2011 6:10 AM | share tips intraday
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: