Windows Transparent Background


   
  

<Window x:Class="Windows.TransparentBackground"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Windows" Height="300" Width="300"
    WindowStyle="None" AllowsTransparency="true">
  <Window.Background>    
    <ImageBrush ImageSource="c:image.png"></ImageBrush>
  </Window.Background>
    <Grid>
      <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
      </Grid.RowDefinitions>
      <Button Margin="20" Grid.Row="2">Close</Button>   
    </Grid>
</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;

namespace Windows
{
    public partial class TransparentBackground : System.Windows.Window
    {
        public TransparentBackground()
        {
            InitializeComponent();
        }

    }
}

   
    
     


Save Window Position to Registry


   
  

<Window x:Class="Windows.SavePosition"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="SavePosition" Height="300" Width="300" >
  <StackPanel Margin="10">
    <Button Click="cmdSave_Click">Save Position</Button>
    <Button Click="cmdRestore_Click">Restore Position</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 Microsoft.Win32;
 
namespace Windows
{
    public partial class SavePosition : System.Windows.Window
    {
        public SavePosition()
        {
            InitializeComponent();
        }

        private void cmdSave_Click(object sender, RoutedEventArgs e)
        {
            WindowPositionHelper.SaveSize(this);
        }

        private void cmdRestore_Click(object sender, RoutedEventArgs e)
        {
            WindowPositionHelper.SetSize(this);
        }
    }
    public class WindowPositionHelper
    {
        public static string RegPath = @"SoftwareMyApp";
        
        public static void SaveSize(Window win)
        {
            RegistryKey key = Registry.CurrentUser.CreateSubKey(RegPath + win.Name);
            key.SetValue("Bounds", win.RestoreBounds.ToString());            
        }

        public static void SetSize(Window win)
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey(RegPath + win.Name);
            if (key != null)
            {
                Rect bounds = Rect.Parse(key.GetValue("Bounds").ToString());

                win.Top = bounds.Top;
                win.Left = bounds.Left;
                if (win.SizeToContent == SizeToContent.Manual)
                {
                    win.Width = bounds.Width;
                    win.Height = bounds.Height;
                }
            }
        }
    }

}

   
    
     


Show window based on button name


   
  
<Window x:Class="LayoutPanels.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Menu" Height="375.2" Width="252">
    <Grid>
      <StackPanel Margin="5" Button.Click="ButtonClick">
        <Button>SimpleStack</Button>
        <Button>SimpleWrap</Button>
        <Button>SimpleDock</Button>
        <Button>BasicDialogBox</Button>
        <Button>SimpleGrid</Button>
        <Button>SplitWindow</Button>
        <Button>DoubleSplitWindow</Button>
        <Button>SharedSizeGroup</Button>
        <Button>SimpleCanvas</Button>
        <Button>SimpleInkCanvas</Button>
        <Button>TheUniformGrid</Button>
        <Button>TextBoxColumn</Button>
        <Button>LocalizableText</Button>
        <Button>ModularContent</Button>
      </StackPanel>
        
    </Grid>
</Window>

//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Reflection;


namespace LayoutPanels
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void ButtonClick(object sender, RoutedEventArgs e)
        {
            Button cmd = (Button)e.OriginalSource;
                
            Type type = this.GetType();
            Assembly assembly = type.Assembly;                       
            Window win = (Window)assembly.CreateInstance("YourNameSpace." + cmd.Content);

            win.ShowDialog();
        }
    }
}

   
    
     


Listen to Window loaded event


   
  

<Window x:Class="WpfApplication1.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title=" " Height="300" Width="300" Loaded="Window1_Loaded">
  <Grid>
    <Viewbox>
      <TextBox x:Name="tbxEditMe" Text="Edit Me..." />
    </Viewbox>      
  </Grid>
</Window>


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

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void Window1_Loaded(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("", "loaded");
            
        }
    }
}

   
    
     


Create Window and add window closing event handler


//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

namespace WpfApplication1
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void Window1_Loaded(object sender, RoutedEventArgs e)
{
Brush[] backgrounds = new Brush[5]{ Brushes.Red,
Brushes.Blue,
Brushes.Green,
Brushes.Yellow,
Brushes.HotPink};
for (int i = 1; i <= 5; i++) { Window window = new Window(); SetupWindow(window, "Window " + i, backgrounds[i - 1]); window.Show(); } } private void SetupWindow(Window window, string title, Brush background) { window.Closing += new CancelEventHandler(Window_Closing); window.Closed += new EventHandler(Window_Closed); window.Title = title; window.Width = 100d; window.Height = 100d; Viewbox viewBox = new Viewbox(); TextBlock textBlock = new TextBlock(); window.Background = background; viewBox.Child = textBlock; textBlock.Text = window.Title; window.Content = viewBox; } private void Window_Closed(object sender, EventArgs e) { Console.WriteLine("closed"); } private void Window_Closing(object sender, CancelEventArgs e) { Window w = sender as Window; if (w == null) return; } } } [/csharp]

Activate window, close window, bring window to front


   
  

<Window x:Class="WpfApplication1.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:system="clr-namespace:System.Windows;assembly=PresentationFramework"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Main Window" Height="310" Width="280" Loaded="Window1_Loaded">
  <Window.Resources>
    <DataTemplate DataType="{x:Type Window}" x:Key="WindowTemplate">
      <StackPanel>
        <Rectangle Height="50" Width="50">
          <Rectangle.Fill>
            <VisualBrush Visual="{Binding}" />
          </Rectangle.Fill>
        </Rectangle>
        <TextBlock Text="{Binding Path=Title}" />
      </StackPanel>
    </DataTemplate>
  </Window.Resources>
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="100" />
      <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <ListBox x:Name="lbxWindows" ItemTemplate="{StaticResource WindowTemplate}">
      <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
          <WrapPanel />
        </ItemsPanelTemplate>
      </ListBox.ItemsPanel>
    </ListBox>

    <StackPanel Grid.Row="1">
      <CheckBox x:Name="cbxIsVisibleInTaskBar" Content="IsVisibleInTaskbar" IsChecked="{Binding ElementName=lbxWindows, Path=SelectedItem.ShowInTaskbar}"/>
      <CheckBox x:Name="cbxIsVisible" Content="IsVisible" 
      IsChecked="{Binding ElementName=lbxWindows, Path=SelectedItem.IsVisible, Mode=OneWay}" 
      Checked="CheckBox_Checked_Changed" 
        Unchecked="CheckBox_Checked_Changed"/>
      <CheckBox x:Name="cbxCanClose" Content="CanClose" IsChecked="True"/>
      <Button Content="Bring To Front" Click="btnBringToFront_Click" />
      <Button Content="Close" Click="btnClose_Click"/>
    </StackPanel>
  </Grid>
</Window>
//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void Window1_Loaded(object sender, RoutedEventArgs e)
        {
            for (int i = 1; i <= 5; i++)
            {
                Window window = new Window();

                SetupWindow(window, "Window " + i);

                window.Show();
            }
            
            RebuildWindowList();
        }
        private void SetupWindow(Window window, string title)
        {
            window.Closing += new CancelEventHandler(Window_Closing);
            window.Closed += new EventHandler(Window_Closed);
            window.Title = title;
            window.Width = 100d;
            window.Height = 100d;
            window.Content = title;
        }
        private void RebuildWindowList()
        {
            List<Window> windows = new List<Window>();

            foreach (Window window in Application.Current.Windows)
            {
                if (window == this)
                    continue;

                windows.Add(window);
            }

            lbxWindows.ItemsSource = windows;
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            RebuildWindowList();
        }

        private void Window_Closing(object sender, CancelEventArgs e)
        {
            Window w = sender as Window;

            if (w == null)
                return;

            e.Cancel = !cbxCanClose.IsChecked == true;
        }

        private void CheckBox_Checked_Changed(object sender, RoutedEventArgs e)
        {
            Window window = lbxWindows.SelectedItem as Window;

            if (window == null) 
                return;

            if (cbxIsVisible.IsChecked == true)
                window.Show();
            else
                window.Hide();
        }

        private void btnBringToFront_Click(object sender, RoutedEventArgs e)
        {
            Window window = lbxWindows.SelectedItem as Window;

            if (window != null)
                window.Activate();
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            Window window = lbxWindows.SelectedItem as Window;

            if (window != null)
                window.Close();
        }
    }
}

   
    
     


Do not handle events until Window is fully initialized.


   
  
<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" Height="100" Width="300">
    <StackPanel>
        <ComboBox Name="comboBox" IsEditable="True" Margin="5" SelectionChanged="ComboBox_SelectionChanged">
            <ComboBoxItem Content="ComboBox Item 1" Selected="ComboBoxItem_Selected" />
            <ComboBoxItem Content="ComboBox Item 2" Selected="ComboBoxItem_Selected" />
            <ComboBoxItem Content="ComboBox Item 3" Selected="ComboBoxItem_Selected" IsSelected="True"/>
            <ComboBoxItem Content="ComboBox Item 4" Selected="ComboBoxItem_Selected" />
            <ComboBoxItem Content="ComboBox Item 5" Selected="ComboBoxItem_Selected" />
        </ComboBox>
        <Button Content="Get Selected" Margin="5" Width="100" Click="Button_Click" />
    </StackPanel>
</Window>

//File:Window.xaml.cs
using System;
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)
        {
            ComboBoxItem item = comboBox.SelectedItem as ComboBoxItem;

            if (item != null)
            {
                MessageBox.Show("Current item: " + item.Content, Title);
            }
            else if (!String.IsNullOrEmpty(comboBox.Text))
            {
                MessageBox.Show("Text entered: " + comboBox.Text, Title);
            }
        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            if (!IsInitialized) return;

            ComboBoxItem item = comboBox.SelectedItem as ComboBoxItem;

            if (item != null)
            {
                MessageBox.Show("Selected item: " + item.Content, Title);
            }
        }

        private void ComboBoxItem_Selected(object sender,RoutedEventArgs e)
        {
            if (!IsInitialized) return;

            ComboBoxItem item = e.OriginalSource as ComboBoxItem;

            if (item != null)
            {
                MessageBox.Show(item.Content + " was selected.", Title);
            }
        }
    }
}