WrapPanel with Background


   
    

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  HorizontalAlignment="Center" VerticalAlignment="Center" Width="150">
<WrapPanel Background="Beige">
  <Button>One</Button>
  <Button>Two</Button>
  <Button>Three</Button>
  <Button>Four</Button>
  <Button>Five</Button>
  <Button>Six</Button>
  <Button>Seven</Button>
  <Button>Eight</Button>
</WrapPanel>
</Page>

   
    
    
    
     


FlowDirection of WrapPanel


   
    

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  HorizontalAlignment="Center" VerticalAlignment="Center" Width="170">
<StackPanel>
  <WrapPanel Orientation="Horizontal">
    <Button>One</Button>
    <Button>Two</Button>
    <Button>Three</Button>
  </WrapPanel>
  <WrapPanel Orientation="Horizontal" FlowDirection="RightToLeft">
    <Button>One</Button>
    <Button>Two</Button>
    <Button>Three</Button>
  </WrapPanel>
</StackPanel>
</Page>

   
    
    
    
     


Simple WrapPanel


   
    

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Fun with Panels!" Height="284" Width="323">
  <WrapPanel Background="LightSteelBlue">
    <Label Name="lblInstruction" Width="328" Height="27" FontSize="15">Enter Car Information</Label>
    <Label Name="lblMake">Make</Label>
    <TextBox Name="txtMake" Width="193" Height="25"/>
    <Label Name="lblColor">Color</Label>
    <TextBox Name="txtColor" Width="193" Height="25"/>
    <Label Name="lblPetName">Pet Name</Label>
    <TextBox Name="txtPetName" Width="193" Height="25"/>
    <Button Name="btnOK" Width="80">OK</Button>
  </WrapPanel>
</Window>

   
    
    
    
     


DataContext with user defined object

   
  

<Window x:Class="CustomDialogSample.SettingsDialog"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Settings"
  Height="200"
  Width="400"
  ResizeMode="CanResizeWithGrip"
  SizeToContent="WidthAndHeight"
  WindowStartupLocation="CenterOwner"
  FocusManager.FocusedElement="{Binding ElementName=myStringTextBox}"
  ShowInTaskbar="False">

  <StackPanel>
    <Label Target="{Binding ElementName=myStringTextBox}">Report _Folder</Label>
    <TextBox x:Name="myStringTextBox" Text="{Binding MyString}" />
    <Button x:Name="folderBrowseButton">...</Button>
    <Button HorizontalAlignment="Left" Name="reportColorButton">
      <StackPanel>
      <Rectangle Width="15" Height="15" SnapsToDevicePixels="True"
                   Fill="{StaticResource reportBrush}" />
        <AccessText Text="Report _Color..." Margin="10,0,0,0" />
      </StackPanel>
    </Button>
    <Button x:Name="okButton" IsDefault="True">OK</Button>
    <Button x:Name="cancelButton" IsCancel="True">Cancel</Button>
  </StackPanel>
</Window>
//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;


namespace CustomDialogSample {
    class DialogData : INotifyPropertyChanged {
      Color reportColor;
      public Color MyColor {
        get { return reportColor; }
        set { reportColor = value; Notify("MyColor"); }
      }

      string mystring;
      public string MyString {
        get { return mystring; }
        set { mystring = value; Notify("MyString"); }
      }

      public event PropertyChangedEventHandler PropertyChanged;
      void Notify(string prop) { if( PropertyChanged != null ) { PropertyChanged(this, new PropertyChangedEventArgs(prop)); } }
    }
  public partial class SettingsDialog : System.Windows.Window {
    DialogData data = new DialogData();

    public Color MyColor {
      get { return data.MyColor; }
      set { data.MyColor = value; }
    }

    public string MyString {
      get { return data.MyString; }
      set { data.MyString = value; }
    }

    public SettingsDialog() {
      InitializeComponent();

      DataContext = data;

      reportColorButton.Click += reportColorButton_Click;
      folderBrowseButton.Click += folderBrowseButton_Click;
      okButton.Click += new RoutedEventHandler(okButton_Click);
    }

    void okButton_Click(object sender, RoutedEventArgs e) {
      DialogResult = true;
    }
    void reportColorButton_Click(object sender, RoutedEventArgs e) {
      System.Windows.Forms.ColorDialog dlg = new System.Windows.Forms.ColorDialog();
      Color color = MyColor;
      dlg.Color = System.Drawing.Color.FromArgb(color.A, color.R, color.G, color.B);

      if( dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK ) {
        MyColor = Color.FromArgb(dlg.Color.A, dlg.Color.R, dlg.Color.G, dlg.Color.B);
      }
    }

    void folderBrowseButton_Click(object sender, RoutedEventArgs e) {
      System.Windows.Forms.FolderBrowserDialog dlg = new System.Windows.Forms.FolderBrowserDialog();
      dlg.SelectedPath = MyString;
      if( dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK ) {
        MyString = dlg.SelectedPath;
      }
    }

  }
}

   
    
     


Set the DataContext of a Window to a person object


   
  

<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 WpfApplication1" Height="180" Width="260">
    <StackPanel>
        <TextBlock Text="Last Name" VerticalAlignment="Center"/>
        <TextBox Text="{Binding Path=LastName, Mode=TwoWay}"/>
        
        <TextBlock Text="Age" VerticalAlignment="Center"/>
        <TextBox Text="{Binding Path=Age, Mode=TwoWay}"/>
        
        <TextBlock Text="Occupation" VerticalAlignment="Center"/>
        <ComboBox x:Name="cboOccupation" IsEditable="False" HorizontalAlignment="Left"
            Text="{Binding Path=Occupation, Mode=TwoWay}"
            Margin="4" Width="140">
             <ComboBoxItem>Student</ComboBoxItem>
             <ComboBoxItem>Skilled</ComboBoxItem>
             <ComboBoxItem>Professional</ComboBoxItem>
        </ComboBox>
                  
        <TextBlock Margin="4" Text="Description" FontWeight="Bold" FontStyle="Italic" VerticalAlignment="Center"/>
        <TextBlock Margin="4" Text="{Binding Path=Description, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center"/>
        
    </StackPanel>
</Window>
//File:Window.xaml.cs
using System.Windows;
using System.ComponentModel;
namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            
            this.DataContext = new Employee(){
                        LastName = "B",
                        Age = 26,
                        Occupation = "Professional"
            };
        }
    }

    public class Employee : INotifyPropertyChanged
    {
        private string lastName;
        private int age;
        private string occupation;

        public string LastName
        {
            get
            {
                return lastName;
            }
            set
            {
                if(this.lastName != value)
                {
                    this.lastName = value;
                    OnPropertyChanged("LastName");
                    OnPropertyChanged("Description");
                }
            }
        }

        public int Age
        {
            get
            {
                return age;
            }
            set
            {
                if(this.age != value)
                {
                    this.age = value;
                    OnPropertyChanged("Age");
                    OnPropertyChanged("Description");
                }
            }
        }
        
        public string Occupation
        {
            get { return occupation; }
            set
            {
                if (this.occupation != value)
                {
                    this.occupation = value;
                    OnPropertyChanged("Occupation");
                    OnPropertyChanged("Description");
                }
            }
        }

        public string Description
        {
            get
            {
                return string.Format("{0} {1},  ({2})", 
                                      lastName, age, occupation);
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            if(this.PropertyChanged != null)
            {

                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

    }
}

   
    
     


ResizeMode=CanResizeWithGrip

   
  

<Window x:Class="CustomDialogSample.SettingsDialog"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Settings"
  Height="200"
  Width="400"
  ResizeMode="CanResizeWithGrip"
  SizeToContent="WidthAndHeight"
  WindowStartupLocation="CenterOwner"
  FocusManager.FocusedElement="{Binding ElementName=myStringTextBox}"
  ShowInTaskbar="False">

  <StackPanel>
    <Label Target="{Binding ElementName=myStringTextBox}">Report _Folder</Label>
    <TextBox x:Name="myStringTextBox" Text="{Binding MyString}" />
    <Button x:Name="folderBrowseButton">...</Button>
    <Button HorizontalAlignment="Left" Name="reportColorButton">
      <StackPanel>
      <Rectangle Width="15" Height="15" SnapsToDevicePixels="True"
                   Fill="{StaticResource reportBrush}" />
        <AccessText Text="Report _Color..." Margin="10,0,0,0" />
      </StackPanel>
    </Button>
    <Button x:Name="okButton" IsDefault="True">OK</Button>
    <Button x:Name="cancelButton" IsCancel="True">Cancel</Button>
  </StackPanel>
</Window>
//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;


namespace CustomDialogSample {
    class DialogData : INotifyPropertyChanged {
      Color reportColor;
      public Color MyColor {
        get { return reportColor; }
        set { reportColor = value; Notify("MyColor"); }
      }

      string mystring;
      public string MyString {
        get { return mystring; }
        set { mystring = value; Notify("MyString"); }
      }

      public event PropertyChangedEventHandler PropertyChanged;
      void Notify(string prop) { if( PropertyChanged != null ) { PropertyChanged(this, new PropertyChangedEventArgs(prop)); } }
    }
  public partial class SettingsDialog : System.Windows.Window {
    DialogData data = new DialogData();

    public Color MyColor {
      get { return data.MyColor; }
      set { data.MyColor = value; }
    }

    public string MyString {
      get { return data.MyString; }
      set { data.MyString = value; }
    }

    public SettingsDialog() {
      InitializeComponent();

      DataContext = data;

      reportColorButton.Click += reportColorButton_Click;
      folderBrowseButton.Click += folderBrowseButton_Click;
      okButton.Click += new RoutedEventHandler(okButton_Click);
    }

    void okButton_Click(object sender, RoutedEventArgs e) {
      DialogResult = true;
    }
    void reportColorButton_Click(object sender, RoutedEventArgs e) {
      System.Windows.Forms.ColorDialog dlg = new System.Windows.Forms.ColorDialog();
      Color color = MyColor;
      dlg.Color = System.Drawing.Color.FromArgb(color.A, color.R, color.G, color.B);

      if( dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK ) {
        MyColor = Color.FromArgb(dlg.Color.A, dlg.Color.R, dlg.Color.G, dlg.Color.B);
      }
    }

    void folderBrowseButton_Click(object sender, RoutedEventArgs e) {
      System.Windows.Forms.FolderBrowserDialog dlg = new System.Windows.Forms.FolderBrowserDialog();
      dlg.SelectedPath = MyString;
      if( dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK ) {
        MyString = dlg.SelectedPath;
      }
    }

  }
}

   
    
     


SizeToContent=WidthAndHeight

   
  

<Window x:Class="CustomDialogSample.SettingsDialog"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Settings"
  Height="200"
  Width="400"
  ResizeMode="CanResizeWithGrip"
  SizeToContent="WidthAndHeight"
  WindowStartupLocation="CenterOwner"
  FocusManager.FocusedElement="{Binding ElementName=myStringTextBox}"
  ShowInTaskbar="False">

  <StackPanel>
    <Label Target="{Binding ElementName=myStringTextBox}">Report _Folder</Label>
    <TextBox x:Name="myStringTextBox" Text="{Binding MyString}" />
    <Button x:Name="folderBrowseButton">...</Button>
    <Button HorizontalAlignment="Left" Name="reportColorButton">
      <StackPanel>
      <Rectangle Width="15" Height="15" SnapsToDevicePixels="True"
                   Fill="{StaticResource reportBrush}" />
        <AccessText Text="Report _Color..." Margin="10,0,0,0" />
      </StackPanel>
    </Button>
    <Button x:Name="okButton" IsDefault="True">OK</Button>
    <Button x:Name="cancelButton" IsCancel="True">Cancel</Button>
  </StackPanel>
</Window>
//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;


namespace CustomDialogSample {
    class DialogData : INotifyPropertyChanged {
      Color reportColor;
      public Color MyColor {
        get { return reportColor; }
        set { reportColor = value; Notify("MyColor"); }
      }

      string mystring;
      public string MyString {
        get { return mystring; }
        set { mystring = value; Notify("MyString"); }
      }

      public event PropertyChangedEventHandler PropertyChanged;
      void Notify(string prop) { if( PropertyChanged != null ) { PropertyChanged(this, new PropertyChangedEventArgs(prop)); } }
    }
  public partial class SettingsDialog : System.Windows.Window {
    DialogData data = new DialogData();

    public Color MyColor {
      get { return data.MyColor; }
      set { data.MyColor = value; }
    }

    public string MyString {
      get { return data.MyString; }
      set { data.MyString = value; }
    }

    public SettingsDialog() {
      InitializeComponent();

      DataContext = data;

      reportColorButton.Click += reportColorButton_Click;
      folderBrowseButton.Click += folderBrowseButton_Click;
      okButton.Click += new RoutedEventHandler(okButton_Click);
    }

    void okButton_Click(object sender, RoutedEventArgs e) {
      DialogResult = true;
    }
    void reportColorButton_Click(object sender, RoutedEventArgs e) {
      System.Windows.Forms.ColorDialog dlg = new System.Windows.Forms.ColorDialog();
      Color color = MyColor;
      dlg.Color = System.Drawing.Color.FromArgb(color.A, color.R, color.G, color.B);

      if( dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK ) {
        MyColor = Color.FromArgb(dlg.Color.A, dlg.Color.R, dlg.Color.G, dlg.Color.B);
      }
    }

    void folderBrowseButton_Click(object sender, RoutedEventArgs e) {
      System.Windows.Forms.FolderBrowserDialog dlg = new System.Windows.Forms.FolderBrowserDialog();
      dlg.SelectedPath = MyString;
      if( dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK ) {
        MyString = dlg.SelectedPath;
      }
    }

  }
}