Geeks With Blogs
Braulio Díez Botella blog
 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 
Posted on Saturday, September 26, 2009 4:44 PM | Back to top


Comments on this post: StackPanel IsEnabled

# re: StackPanel IsEnabled
Requesting Gravatar...
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
Left by Sebastian on Sep 27, 2009 10:52 AM

# re: StackPanel IsEnabled
Requesting Gravatar...
This is exactly the solution that I needed. Thanks! :)
Left by SoftwareDeveloper on Aug 16, 2010 8:21 PM

# Mr
Requesting Gravatar...
I really enjoy my visit to this site really very nice writing
Left by share tips intraday on Jan 06, 2011 6:10 AM

Your comment:
 (will show your gravatar)


Copyright © Braulio Díez Botella | Powered by: GeeksWithBlogs.net