Tag Archives: wpf

[WPF][MVVM] TreeView: Scroll To Selected Item

Advertisement from Google Here is the MVVM way to bring selected TreeViewItem into a view. First we need an attached behavior. We cannot use regular behavior because we will attach this property through Style. public class BringSelectedItemIntoViewBehavior { public static readonly DependencyProperty IsBringSelectedIntoViewProperty = DependencyProperty.RegisterAttached( "IsBringSelectedIntoView", typeof (bool), typeof (BringSelectedItemIntoViewBehavior), new PropertyMetadata(default(bool), PropertyChangedCallback)); public static void… Read More »

[WPF] Binding ItemsSource to Enum

Suppose you need to bind ItemsSource dependency property to enum’s values. For example in ComboBox. You have following enum: public enum ExampleEnum { Red, Green, Yellow } Now you can use ObjectDataProvider <Window x:Class="ExampleApplication.Window" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:System="clr-namespace:System;assembly=mscorlib" xmlns:local="clr-namespace:ExampleApplication" Title="Bind to Enum" Height="250" Width="250"> <Window.Resources> <ObjectDataProvider x:Key="enumValues" MethodName="GetValues" ObjectType="{x:Type System:Enum}"> <ObjectDataProvider.MethodParameters> <x:Type TypeName="local:ExampleEnum"/> </ObjectDataProvider.MethodParameters> </ObjectDataProvider> </Window.Resources>… Read More »

[WPF] ContextMenu Commnad binding issue fixed

If there is no focused element in the window’s main focus scope, the CanExecute routing will stop at the ContextMenu, so it will not reach to the CommandBinding on the Window, one workaround is to bind MenuItem’s CommandTarget to the main window. <ContextMenu > <ContextMenu.Items> <MenuItem Command="ApplicationCommands.Open" CommandTarget="{Binding Path=PlacementTarget,RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"/> </ContextMenu.Items> </ContextMenu> (c) Marco… Read More »

[WPF] Auto Scroll Behavior for ListBox

If you want scroll ListBox to the end automatically when new item added in ItemsSource collection you could use following attached behavior. This approach perfectly suit for MVVM way. ListBoxBehavior.cs: using System.Windows; namespace ListBoxBehavior { /// <summary> /// ListBox AutoScroll attached properties /// </summary> public static class ListBoxBehavior { public static readonly DependencyProperty AutoScrollProperty = DependencyProperty.RegisterAttached( "AutoScroll",… Read More »

Compare performace of WPF Converters, DataTriggers and Direct Binding

Hello, I have made some performance tests with DataTriggers and Converters. For testing purposes I have chosen Visibility property. In first case I have binded Visibility property to IsVisible view model boolean property with standard BooleanToVisibilityConverter converter. <Border Visibility="{Binding IsVisible, Converter={StaticResource BooleanToVisibilityConverter}}" Background="Blue" /> Next case was written with DataTrigger <Border Background="Red"> <Border.Style> <Style TargetType="Border">… Read More »

WPF Binding examples

I have found very nice WPF XAML Data Binding Cheat Sheet, so I’ll just leave it here for further use. Basic Binding {Binding} Bind to current DataContext. {Binding Name} Bind to the “Name” property of the current DataContext. {Bindind Name.Length} Bind to the Length property of the object in the Name property of the current DataContext.… Read More »

Оптимизации в WPF MultiDataTrigger

Использовал я однажды MultiDataTrigger в WPF приложении, и стало мне интересно а применяется ли для условий оптимизация на подобии оператора && в C#, когда второй операнд вычисляется только если первый равен true. Напомню, для того что бы сработал MultiDataTrigger необходиом выполнение всех условий заданных в блоке Conditions: <Style TargetType="Grid"> <Setter Property="Background" Value="MediumAquamarine" /> <Style.Triggers> <MultiDataTrigger> <MultiDataTrigger.Conditions> <Condition… Read More »