Set Item Focus in ListView WPF

There are two types of focus in WPF – Keyboard Focus and Logical Focus. This link can give you more information about focus in WPF. You can either do this: ListViewItem item = myListView.ItemContainerGenerator.ContainerFromIndex(index) as ListViewItem; item.Focus(); It’s also possible to call Keyboard.Focus(item); If you also want to scroll the ListView to the item’s position, … Read more

How to display items in Canvas through Binding

Set the ItemsPanel to a Canvas and bind the containers instead of the TextBlock in the DataTemplate <ItemsControl ItemsSource=”{Binding Path=ItemsToShowInCanvas}”> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <Canvas/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemContainerStyle> <Style TargetType=”ContentPresenter”> <Setter Property=”Canvas.Left” Value=”{Binding Left}”/> <Setter Property=”Canvas.Top” Value=”{Binding Top}”/> </Style> </ItemsControl.ItemContainerStyle> <ItemsControl.ItemTemplate> <DataTemplate> <TextBlock Text=”{Binding Path=Text}” /> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl>

Binding as a Resource

Direct answer to your question is “yes, you can define a binding as a resource”. The problem here is how do you then make any use of it? One possibility is to create an extension class which would pull the binding from the resources and apply it: public class BindingResourceExtension : StaticResourceExtension { public BindingResourceExtension() … Read more

Force rendering of a WPF control in memory

use a ViewBox to render in memory Grid grid = new System.Windows.Controls.Grid() { Background = Brushes.Blue, Width = 200, Height = 200 }; Viewbox viewbox = new Viewbox(); viewbox.Child = grid; //control to render viewbox.Measure(new System.Windows.Size(200, 200)); viewbox.Arrange(new Rect(0, 0, 200, 200)); viewbox.UpdateLayout(); RenderTargetBitmap render = new RenderTargetBitmap(200, 200, 150, 150, PixelFormats.Pbgra32); render.Render(viewbox);

How to bind Close command to a button

All it takes is a bit of XAML… <Window x:Class=”WCSamples.Window1″ xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”> <Window.CommandBindings> <CommandBinding Command=”ApplicationCommands.Close” Executed=”CloseCommandHandler”/> </Window.CommandBindings> <StackPanel Name=”MainStackPanel”> <Button Command=”ApplicationCommands.Close” Content=”Close Window” /> </StackPanel> </Window> And a bit of C#… private void CloseCommandHandler(object sender, ExecutedRoutedEventArgs e) { this.Close(); } (adapted from this MSDN article)