Change Height


   
  

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="Height_MinHeight_MaxHeight.Window1"
    Title="Height Properties Sample">
  <StackPanel Margin="10">
      <Canvas Height="200" MinWidth="200" Name="myCanvas">
           <Rectangle HorizontalAlignment="Center" Canvas.Top="50" Canvas.Left="50"  Name="rect1" Fill="#4682b4" Height="100" Width="100"/>
      </Canvas>
      <Button Name="Button1" Click="clipRect">Canvas.ClipToBounds="True"</Button>
      <Button Name="Button2" Margin="0,5,5,5" Click="unclipRect">Canvas.ClipToBounds="False"</Button>
    <TextBlock Grid.Row="1" Grid.Column="0" Margin="10,0,0,0" TextWrapping="Wrap">Set the Rectangle Height:</TextBlock>
    <ListBox Grid.Column="1" Grid.Row="1" Margin="10,0,0,0" Height="50" Width="50" SelectionChanged="changeHeight">
      <ListBoxItem>25</ListBoxItem>
      <ListBoxItem>50</ListBoxItem>
      <ListBoxItem>75</ListBoxItem>
      <ListBoxItem>100</ListBoxItem>
      <ListBoxItem>125</ListBoxItem>
      <ListBoxItem>150</ListBoxItem>
      <ListBoxItem>175</ListBoxItem>
      <ListBoxItem>200</ListBoxItem>
    </ListBox>

  </StackPanel>
</Window>

//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Documents;

namespace Height_MinHeight_MaxHeight
{
  public partial class Window1 : Window
    {
        public void changeHeight(object sender, SelectionChangedEventArgs args)
        {
            ListBoxItem li = ((sender as ListBox).SelectedItem as ListBoxItem);
            Double sz1 = Double.Parse(li.Content.ToString());
            rect1.Height = sz1;
            rect1.UpdateLayout();
            Console.WriteLine("ActualHeight is set to " + rect1.ActualHeight);
            Console.WriteLine("Height is set to " + rect1.Height);
            Console.WriteLine("MinHeight is set to " + rect1.MinHeight);
            Console.WriteLine("MaxHeight is set to " + rect1.MaxHeight);
        }


        public void clipRect(object sender, RoutedEventArgs args)
        {
            myCanvas.ClipToBounds = true;
            Console.WriteLine("Canvas.ClipToBounds is set to " + myCanvas.ClipToBounds);
        }
        public void unclipRect(object sender, RoutedEventArgs args)
        {
            myCanvas.ClipToBounds = false;
            Console.WriteLine("Canvas.ClipToBounds is set to " + myCanvas.ClipToBounds);
        }
    }
}

   
    
     


Change MinHeight


   
  

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="Height_MinHeight_MaxHeight.Window1"
    Title="Height Properties Sample">
  <StackPanel Margin="10">
      <Canvas Height="200" MinWidth="200" Name="myCanvas">
           <Rectangle HorizontalAlignment="Center" Canvas.Top="50" Canvas.Left="50"  Name="rect1" Fill="#4682b4" Height="100" Width="100"/>
      </Canvas>
      <Button Name="Button1" Click="clipRect">Canvas.ClipToBounds="True"</Button>
      <Button Name="Button2" Margin="0,5,5,5" Click="unclipRect">Canvas.ClipToBounds="False"</Button>
  <TextBlock Grid.Row="1" Grid.Column="2" Margin="10,0,0,0" TextWrapping="Wrap">Set the Rectangle MinHeight:</TextBlock>
    <ListBox Grid.Column="3" Grid.Row="1" Margin="10,0,0,0" Height="50" Width="50" SelectionChanged="changeMinHeight">
      <ListBoxItem>25</ListBoxItem>
      <ListBoxItem>50</ListBoxItem>
      <ListBoxItem>75</ListBoxItem>
      <ListBoxItem>100</ListBoxItem>
      <ListBoxItem>125</ListBoxItem>
      <ListBoxItem>150</ListBoxItem>
      <ListBoxItem>175</ListBoxItem>
      <ListBoxItem>200</ListBoxItem>
  </ListBox>    

  </StackPanel>
</Window>

//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Documents;

namespace Height_MinHeight_MaxHeight
{
  public partial class Window1 : Window
    {
        public void changeMinHeight(object sender, SelectionChangedEventArgs args)
        {
            ListBoxItem li = ((sender as ListBox).SelectedItem as ListBoxItem);
            Double sz1 = Double.Parse(li.Content.ToString());
            rect1.MinHeight = sz1;
            rect1.UpdateLayout();
            Console.WriteLine("ActualHeight is set to " + rect1.ActualHeight);
            Console.WriteLine("Height is set to " + rect1.Height);
            Console.WriteLine("MinHeight is set to " + rect1.MinHeight);
            Console.WriteLine("MaxHeight is set to " + rect1.MaxHeight);
        }


        public void clipRect(object sender, RoutedEventArgs args)
        {
            myCanvas.ClipToBounds = true;
            Console.WriteLine("Canvas.ClipToBounds is set to " + myCanvas.ClipToBounds);
        }
        public void unclipRect(object sender, RoutedEventArgs args)
        {
            myCanvas.ClipToBounds = false;
            Console.WriteLine("Canvas.ClipToBounds is set to " + myCanvas.ClipToBounds);
        }
    }
}

   
    
     


RadioButton checked event handler


   
 
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" SizeToContent="Height" Width="300">
    <Grid Name="grid">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Border Grid.Column="0" BorderBrush="Gray" BorderThickness="1" />
        <Border Grid.Column="1" BorderBrush="Gray" BorderThickness="1" />
        <StackPanel Grid.Column="0" HorizontalAlignment="Center" Margin="5" Name="spLeftContainer">
            <TextBlock FontSize="16" Text="Radio Group 1" />
            <RadioButton Content="Radio Button 1A" GroupName="Group1" IsChecked="True" Margin="5" Name="rbnOneA" />
            <RadioButton Content="Radio Button 1B" GroupName="Group1" Margin="5" Name="rbnOneB" />
            <RadioButton Content="Radio Button 1C" GroupName="Group1" Margin="5" Name="rbnOneC" />
            <Separator/>
            <TextBlock FontSize="16" Text="Radio Group 2" />
            <RadioButton Checked="RadioButton_Checked" GroupName="Group2" Content="Radio Button 2A" IsChecked="True" 
                         Margin="5" Name="rbnTwoA" />
            <RadioButton Checked="RadioButton_Checked" GroupName="Group2" Content="Radio Button 2B" Margin="5" Name="rbnTwoB"/>
            <RadioButton Checked="RadioButton_Checked" GroupName="Group2" Content="Radio Button 2C" Margin="5" Name="rbnTwoC"/>
        </StackPanel>
        <StackPanel Grid.Column="1" HorizontalAlignment="Center" Margin="5" Name="spRightContainer">
            <TextBlock FontSize="16" Text="Radio Group 1" />
            <RadioButton Content="Radio Button 1D" GroupName="Group1" Margin="5" Name="rbnOneD" />
            <RadioButton Content="Radio Button 1E" GroupName="Group1" Margin="5" Name="rbnOneE" />
        </StackPanel>
        <Button Content="Show Group1 Selection" Grid.ColumnSpan="2" Grid.Row="1" HorizontalAlignment="Center" 
                Margin="10" MaxHeight="25" Click="Button_Click" />
    </Grid>
</Window>

//File:Window.xaml.cs
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            RadioButton radioButton = null;
            radioButton = GetCheckedRadioButton(spLeftContainer.Children, "Group1");
            if (radioButton == null)
            {
                radioButton = GetCheckedRadioButton(spRightContainer.Children, "Group1");
            }
            MessageBox.Show(radioButton.Content + " checked.", Title);
        }
        private RadioButton GetCheckedRadioButton(UIElementCollection children, String groupName)
        {
            return children.OfType<RadioButton>().FirstOrDefault( rb => rb.IsChecked == true &amp;&amp; rb.GroupName == groupName);
        }
        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            if (!this.IsInitialized) return;
            RadioButton radioButton = e.OriginalSource as RadioButton;

            if (radioButton != null)
            {
                MessageBox.Show(radioButton.Content + " checked.", Title);
            }
        }
    }
}

   
     


A rectangle with curved corners


   
     
<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WpfApplication1" Height="352" Width="334" WindowStartupLocation="CenterScreen">
    <StackPanel>
    <Rectangle RadiusX ="20" RadiusY ="50" Fill ="DarkBlue" Width ="150" Height ="50"/>
    </StackPanel>
</Window>

   
    
    
    
    
     


Paints a rectangle with a grid pattern with VisualBrush


   
     
<Window x:Class="Workspace.DockExample"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Workspace" Width="640" Height="480">
      <Rectangle Width="50" Height="50" Grid.Row="1" Grid.Column="1">
        <Rectangle.Fill>
          <VisualBrush Viewport="0,0,1,0.25" TileMode="Tile" Stretch="Uniform">
            <VisualBrush.Visual>
              <StackPanel Background="White">
                <TextBlock FontSize="10pt" Margin="1">Hello, World!</TextBlock>
              </StackPanel>
            </VisualBrush.Visual>
            <VisualBrush.RelativeTransform>
              <RotateTransform Angle="-45" CenterX="0.5" CenterY="0.5" />
            </VisualBrush.RelativeTransform>
          </VisualBrush>
        </Rectangle.Fill>
      </Rectangle>
</Window>

   
    
    
    
    
     


Paints a rectangle with a checkered pattern.


   
     
<Window x:Class="Workspace.DockExample"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Workspace" Width="640" Height="480">
      <Rectangle Width="150" Height="150">
        <Rectangle.Fill>
          <DrawingBrush Viewport="0,0,0.25,0.25" TileMode="Tile">
            <DrawingBrush.Drawing>
              <DrawingGroup>
                <GeometryDrawing Brush="White">
                  <GeometryDrawing.Geometry>
                    <RectangleGeometry Rect="0,0,1,1" />
                  </GeometryDrawing.Geometry>
                </GeometryDrawing>
                <GeometryDrawing Brush="Black"
                  Geometry="M 0,0 L0,0.5 0.5,0.5 0.5,1 1,1 1,0.5 0.5,0.5 0.5,0" />
              </DrawingGroup>
            </DrawingBrush.Drawing>
          </DrawingBrush>
        </Rectangle.Fill>
      </Rectangle>

</Window>

   
    
    
    
    
     


Paints a rectangle with a grid pattern


   
     

<Window x:Class="Workspace.DockExample"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Workspace" Width="640" Height="480">
      <Rectangle Width="50" Height="50" Grid.Row="1" Grid.Column="1">
        <Rectangle.Fill>
          <DrawingBrush Viewport="0,0,10,10" ViewportUnits="Absolute" TileMode="Tile">
            <DrawingBrush.Drawing>
              <DrawingGroup>
                <GeometryDrawing Brush="White">
                  <GeometryDrawing.Geometry>
                    <RectangleGeometry Rect="0,0,1,1" />
                  </GeometryDrawing.Geometry>
                </GeometryDrawing>
                <GeometryDrawing Brush="Blue"
                  Geometry="M 0,0 L 0,1 0.1,1 0.1,0.1 1,0.1 1,0 Z" />
              </DrawingGroup>
            </DrawingBrush.Drawing>
          </DrawingBrush>
        </Rectangle.Fill>
      </Rectangle>

</Window>