b

Wednesday, 9 January 2013

GroupBox in WPF C#


This example explains How to Add group box to WPF XAML  and Add  2 radio buttons  within groupbox.  Radio buttons express mutually exclusive behavior.

XAML Code

<Page x:Class="Wpfone.GroupBox_demo1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      mc:Ignorable="d"
      d:DesignHeight="300" d:DesignWidth="350"
    Title="GroupBox_demo1">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="60"></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <GroupBox Grid.Row="1" Grid.Column="0" x:Name="group1"
                  Header="Gender" HorizontalAlignment="Left"
                  VerticalAlignment="Top"  Height="71"
                  Width="271" Margin="10,10,0,0">
            <GroupBox.Resources>
                <Style x:Key="radiobuttonStyles" TargetType="RadioButton">
                    <Setter Property="Selector.BorderBrush" Value="Red"></Setter>
                    <Setter Property="BorderThickness" Value="20"></Setter>
                    <Setter Property="Typography.Fraction" Value="Stacked"></Setter>
                    <Setter Property="FontFamily" Value="Comic Sans MS"></Setter>
                    <Setter Property="FontSize" Value="20"></Setter>
                    <Setter Property="FontStretch"  Value="UltraCondensed"></Setter>
                    <Setter Property="BorderBrush" Value="Aqua"></Setter>
                    <Setter Property="BorderThickness" Value="5"></Setter>
                    <Setter Property="Foreground">
                        <Setter.Value>
                            <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                                <LinearGradientBrush.GradientStops>
                                    <GradientStop Offset="0.0" Color="Teal" />
                                    <GradientStop Offset="1.0" Color="GreenYellow" />
                                </LinearGradientBrush.GradientStops>
                            </LinearGradientBrush>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="Typography.Capitals" Value="Titling"></Setter>
                </Style>
            </GroupBox.Resources>
            <StackPanel Orientation="Horizontal">
                <RadioButton x:Name="radioMale" Content="Male"
                             Style="{StaticResource radiobuttonStyles}"
                             HorizontalAlignment="Left" Height="40"
                             VerticalAlignment="Top" Width="112"
                             Margin="10,10,0,0" Checked="radioMale_Checked">

                </RadioButton>
                <RadioButton x:Name="radioFeMale"
                             Style="{StaticResource radiobuttonStyles}"
                             Content="Female" HorizontalAlignment="Left"
                             Height="30" VerticalAlignment="Top"
                             Width="100" Margin="10,10,0,0"
                             Checked="radioFeMale_Checked">

                </RadioButton>


            </StackPanel>
        </GroupBox>
        <TextBlock x:Name="txtTile"  Grid.Column="0" Grid.Row="0"
                   Foreground="WhiteSmoke" FontFamily="Baris Cerin Regular"
                   TextTrimming="CharacterEllipsis"
                    Text="Group Box Demo in WPF &#x0a;
Mutually Exclusive Radio Buttons in WPF"
                   >
            <TextBlock.Background>
                <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                    <LinearGradientBrush.GradientStops>
                        <GradientStop Offset="0.0" Color="Teal" />
                        <GradientStop Offset="1.0" Color="GreenYellow" />
                    </LinearGradientBrush.GradientStops>
                </LinearGradientBrush>
               
            </TextBlock.Background>
            <!--<TextBlock.Text>
            </TextBlock.Text>-->
        </TextBlock>

        <TextBlock x:Name="txtStatus"  Grid.Column="0" Grid.Row="01"  Text=""
                   Foreground="white" VerticalAlignment="Center" Background="Gold" FontFamily="Baris Cerin Regular" TextTrimming="CharacterEllipsis" Margin="20,96,32,62"
                    ></TextBlock>
    </Grid>
</Page>


.CS Code

 private void radioMale_Checked(object sender, RoutedEventArgs e)
        {
            txtStatus.Text = radioMale.Content + " Selected";

        }

        private void radioFeMale_Checked(object sender, RoutedEventArgs e)
        {
            txtStatus.Text = radioFeMale.Content + " Selected";

        }

No comments:

Post a Comment