<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="ImageElementExample.CroppedImageExample" Title="CroppedImage Example" Loaded="PageLoaded"> <Page.Resources> <BitmapImage x:Key="masterImage" UriSource="c:image.jpg" /> <CroppedBitmap x:Key="croppedImage" Source="{StaticResource masterImage}" SourceRect="30 20 105 50"/> </Page.Resources> <StackPanel> <Image Width="200" Source="{StaticResource masterImage}"/> <Image Width="200" Source="{StaticResource croppedImage}"/> <Image Width="200"> <Image.Source> <CroppedBitmap Source="{StaticResource croppedImage}" SourceRect="30 0 75 50"/> </Image.Source> </Image> </StackPanel> </Page> //File:Window.xaml.cs using System; using System.Windows; using System.Windows.Documents; using System.Windows.Controls; using System.Windows.Navigation; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; namespace ImageElementExample { public partial class CroppedImageExample : Page { public CroppedImageExample() { } public void PageLoaded(object sender, RoutedEventArgs args) { Image croppedImage = new Image(); croppedImage.Width = 200; croppedImage.Margin = new Thickness(5); CroppedBitmap cb = new CroppedBitmap((BitmapSource)this.Resources["masterImage"],new Int32Rect(30, 20, 105, 50)); croppedImage.Source = cb; Image chainImage = new Image(); chainImage.Width = 200; CroppedBitmap chained = new CroppedBitmap(cb,new Int32Rect(30, 0, (int)cb.Width-30, (int)cb.Height)); chainImage.Source = chained; } } }
Author: coder
Load Xaml Resource
<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> <Button Name="MyButton" HorizontalAlignment="Center" Margin="24">Button</Button> <Ellipse Width="200" Height="100" Margin="24" Stroke="Red" StrokeThickness="10" /> <ListBox Width="100" Height="100" Margin="24"> <ListBoxItem>Sunday</ListBoxItem> <ListBoxItem>Monday</ListBoxItem> <ListBoxItem>Tuesday</ListBoxItem> </ListBox> </StackPanel> //File:Window.xaml.cs using System; using System.IO; using System.Windows; using System.Windows.Controls; using System.Windows.Markup; namespace MyNameSpace.LoadXamlResource { public class LoadXamlResource : Window { public LoadXamlResource() { Title = "Load Xaml Resource"; Uri uri = new Uri("pack://application:,,,/LoadXamlResource.xml"); Stream stream = Application.GetResourceStream(uri).Stream; FrameworkElement el = XamlReader.Load(stream) as FrameworkElement; Content = el; Button btn = el.FindName("MyButton") as Button; if (btn != null) btn.Click += ButtonOnClick; } void ButtonOnClick(object sender, RoutedEventArgs args) { Console.WriteLine(args.Source.ToString()); } } }
Use Resources.Add to add static resouce from code
<Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="GradientBrushResourceDemo" Height="300" Width="300"> <Window.Resources> <LinearGradientBrush x:Key="brushGradient" StartPoint="0, 0" EndPoint="1, 1"> <LinearGradientBrush.GradientStops> <GradientStop Offset="0" Color="Black" /> <GradientStop Offset="0.5" Color="Green" /> <GradientStop Offset="1" Color="Gold" /> </LinearGradientBrush.GradientStops> </LinearGradientBrush> </Window.Resources> <StackPanel> <TextBlock Margin="{StaticResource thicknessMargin}" Foreground="{StaticResource brushGradient}"> Gradient text </TextBlock> <TextBlock Margin="{StaticResource thicknessMargin}" Foreground="{StaticResource brushGradient}"> Of black, green, and gold </TextBlock> <TextBlock Margin="{StaticResource thicknessMargin}" Foreground="{StaticResource brushGradient}"> Makes an app pretty, </TextBlock> <TextBlock Margin="{StaticResource thicknessMargin}" Foreground="{StaticResource brushGradient}"> Makes an app bold. </TextBlock> </StackPanel> </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; namespace WpfApplication1 { public partial class Window1 : Window { public Window1() { Resources.Add("thicknessMargin", new Thickness(24, 12, 24, 23)); InitializeComponent(); } } }
Event Setter from Resources
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="WpfApplication1.EventSetterDemo" Title="EventSetter Demo"> <Window.Resources> <Style TargetType="{x:Type Button}"> <EventSetter Event="Click" Handler="ButtonOnClick" /> </Style> </Window.Resources> <StackPanel> <Button>Button 1</Button> <Button>Button 2</Button> <Button>Button 3</Button> </StackPanel> </Window> //File:Window.xaml.cs using System; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; namespace WpfApplication1 { public partial class EventSetterDemo : Window { public EventSetterDemo() { InitializeComponent(); } void ButtonOnClick(object sender, RoutedEventArgs args) { Button btn = args.Source as Button; Console.WriteLine(btn.Content); } } }
Programmatically Insert Text into a RichTextBox
<Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="600" Width="800"> <DockPanel> <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal"> <TextBox x:Name="tbxInsertionText" Width="200" Margin="5,0" /> <Button DockPanel.Dock="Bottom" Content="Insert" Click="btnInsert_Click"/> </StackPanel> <RichTextBox x:Name="rtbTextContent" /> </DockPanel> </Window> //File:Window.xaml.cs using System.Windows; using System.Windows.Documents; using System.Windows.Input; namespace WpfApplication1 { public partial class Window1 : Window { public Window1() { InitializeComponent(); } private void btnInsert_Click(object sender, RoutedEventArgs e) { if (string.IsNullOrEmpty(tbxInsertionText.Text)) { return; } rtbTextContent.BeginChange(); if (rtbTextContent.Selection.Text != string.Empty) { rtbTextContent.Selection.Text = string.Empty; } TextPointer tp = rtbTextContent.CaretPosition.GetPositionAtOffset(0, LogicalDirection.Forward); rtbTextContent.CaretPosition.InsertTextInRun(tbxInsertionText.Text); rtbTextContent.CaretPosition = tp; rtbTextContent.EndChange(); Keyboard.Focus(rtbTextContent); } } }
Add event handler to StackPanel in StackPanel resource
<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="150" Width="250"> <StackPanel> <Popup AllowsTransparency="True" Height="100" HorizontalOffset="1cm" Name="myPopup" Placement="Right" StaysOpen="True" Width="200" > <Border BorderBrush="Black" BorderThickness="2"> <DockPanel Background="White" LastChildFill="True"> <TextBlock Background="AliceBlue" DockPanel.Dock="Top" FontSize="16" HorizontalAlignment="Stretch" Margin="5" Text="A WPF Popup" /> <Button Click="btnClosePopup_Click" Content="Close" DockPanel.Dock="Bottom" Margin="5" HorizontalAlignment="Right" MaxHeight="23"/> <Image DockPanel.Dock="Top" Margin="5" Source="c:image.gif" /> </DockPanel> </Border> </Popup> <StackPanel> <StackPanel.Resources> <Style TargetType="{x:Type Button}"> <Setter Property="Margin" Value="2" /> <EventSetter Event="Click" Handler="btnShowPopup_Click" /> </Style> </StackPanel.Resources> <Button Content="Show Popup" Name="btnShowPopup" /> </StackPanel> </StackPanel> </Window> //File:Window.xaml.cs using System; using System.Collections.Generic; using System.Linq; 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.Navigation; using System.Windows.Shapes; using System.Windows.Controls.Primitives; namespace WpfApplication1 { public partial class Window1 : Window { public Window1() { InitializeComponent(); } private void btnClosePopup_Click(object sender, RoutedEventArgs e) { myPopup.IsOpen = false; } private void btnShowPopup_Click(object sender, RoutedEventArgs e) { myPopup.IsOpen = true; } } }
Add Event handler in Panel Resource
<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="200" Width="300"> <DockPanel LastChildFill="True"> <DockPanel.Resources> <Style TargetType="{x:Type TreeViewItem}"> <EventSetter Event="Selected" Handler="TreeViewItem_Selected" /> </Style> </DockPanel.Resources> <Button Click="Button_Click" DockPanel.Dock="Bottom" Content="Show Selected" MaxHeight="23" MaxWidth="100" /> <TreeView FontSize="16" Name="tvTree"> <TreeViewItem Header="A" IsExpanded="True"> <TreeViewItem Header="1"> <TreeViewItem Header="2" /> <TreeViewItem Header="3" /> </TreeViewItem> <TreeViewItem Header="B" IsExpanded="True"> <TreeViewItem Header="11" /> <TreeViewItem Header="22" /> </TreeViewItem> </TreeViewItem> <TreeViewItem Header="C"> <TreeViewItem Header="E"> <TreeViewItem Header="111" /> <TreeViewItem Header="222" /> <TreeViewItem Header="333" /> </TreeViewItem> <TreeViewItem Header="F"> <TreeViewItem Header="1111" /> <TreeViewItem Header="2222" /> <TreeViewItem Header="333" /> </TreeViewItem> </TreeViewItem> </TreeView> </DockPanel> </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 TreeViewItem_Selected(object sender, RoutedEventArgs e) { TreeViewItem item = sender as TreeViewItem; if (item == e.OriginalSource) { Console.WriteLine(item.Header); Console.WriteLine(item.Items.Count); } else { Console.WriteLine("Parent of selected"); Console.WriteLine(item.Header); Console.WriteLine(item.Items.Count); } } private void Button_Click(object sender, RoutedEventArgs e) { TreeViewItem item = tvTree.SelectedItem as TreeViewItem; if (item != null) { MessageBox.Show("Item selected: " + item.Header, Title); } else { MessageBox.Show("No item selected", Title); } } } }