<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="Microsoft.Samples.Graphics.RectangleExample" WindowTitle="Example"> <Canvas> <TextBlock Margin="3">Fixed Tiles</TextBlock> <Rectangle Grid.Column="1" Stroke="Black"> <Rectangle.Fill> <ImageBrush ImageSource="c:image.jpg" TileMode="Tile" ViewportUnits="Absolute" Viewport="0 0 37 37"></ImageBrush> </Rectangle.Fill> </Rectangle> </Canvas> </Page>
Using an ImageBrush
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> <Rectangle> <Rectangle.Fill> <ImageBrush ImageSource="c:image.jpg" /> </Rectangle.Fill> </Rectangle> </Page>
Specifying a Stretch of None for ImageBrush
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> <Rectangle> <Rectangle.Fill> <ImageBrush ImageSource="c:image.jpg" Stretch="None" /> </Rectangle.Fill> </Rectangle> </Page>
Using Viewbox and Viewport
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> <Rectangle> <Rectangle.Fill> <ImageBrush ViewboxUnits="Absolute" Viewbox="300,285,300,243" Viewport="0.1,0.321,0.7, 0.557" ImageSource="c:image.jpg" /> </Rectangle.Fill> </Rectangle> </Page>
TitleMode and Viewport for ImageBrush
<Window x:Class="BrushTransformExample" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Brush Transforms" Height="475" Width="510"> <Grid> <Rectangle Width="120" Height="60" Margin="5" Grid.Column="1" Grid.Row="4"> <Rectangle.Fill> <ImageBrush ImageSource="c:image.jpg" TileMode="Tile" Viewport="0,0,0.5,0.5" /> </Rectangle.Fill> </Rectangle> </Grid> </Window>
Use InputBinding to bind Application commends to Key events
<Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="CommandBasics" Height="300" Width="300"> <Grid> <Button Command="ApplicationCommands.Properties" Content="_Properties"/> </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 WpfApplication1 { public partial class Window1 : Window { public Window1() { InitializeComponent(); InputBinding ib = new InputBinding(ApplicationCommands.Properties,new KeyGesture(Key.Enter, ModifierKeys.Alt)); this.InputBindings.Add(ib); CommandBinding cb = new CommandBinding(ApplicationCommands.Properties); cb.Executed += new ExecutedRoutedEventHandler(cb_Executed); this.CommandBindings.Add(cb); } void cb_Executed(object sender, ExecutedRoutedEventArgs e) { MessageBox.Show("Properties"); } } }
Use InputGestureCollection to get modifier keys
<Window x:Class="Commands.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Commands"> <Grid> <Button VerticalAlignment="Top" HorizontalAlignment="Stretch" Height="27" Click="ExecuteCommandClickEvent" Name="BtnExecuteCommand">Execute Command </Button> </Grid> </Window> //File:Window.xaml.cs using System; using System.Windows; using System.Windows.Controls; using System.Windows.Input; namespace Commands { public partial class Window1 : Window { public static RoutedCommand myCmd; static Window1() { InputGestureCollection myInputs = new InputGestureCollection(); myInputs.Add(new KeyGesture(Key.G,ModifierKeys.Control | ModifierKeys.Shift)); myCmd = new RoutedCommand("Go", typeof(Window1), myInputs); } private void ExecuteCommandClickEvent(object sender, RoutedEventArgs e) { myCmd.Execute(sender,null); } } }