Alternate background color in Listview XAML

I tried this and it works for me. <Window x:Class=”TryResponses.MainWindow” xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml” xmlns:vm=”clr-namespace:TryResponses” xmlns:system=”clr-namespace:System;assembly=mscorlib” Title=”MainWindow” Height=”350″ Width=”525″> <Window.Resources> <vm:MainWindowViewModel x:Key=”MainWindowViewModel” /> </Window.Resources> <Grid Background=”LightGray” DataContext=”{StaticResource MainWindowViewModel}”> <Grid.Resources> <Style TargetType=”ListViewItem”> <Style.Triggers> <Trigger Property=”ItemsControl.AlternationIndex” Value=”0″> <Setter Property=”Background” Value=”LightBlue” /> </Trigger> <Trigger Property=”ItemsControl.AlternationIndex” Value=”1″> <Setter Property=”Background” Value=”LightGray” /> </Trigger> </Style.Triggers> </Style> <DataTemplate DataType=”system:String”> <!– put your data template … Read more

WPF: How to style or disable the default ContextMenu of a TextBox

To style ContextMenu’s for all TextBoxes, I would do something like the following: First, in the resources section, add a ContextMenu which you plan to use as your standard ContextMenu in a textbox. e.g. <ContextMenu x:Key=”TextBoxContextMenu” Background=”White”> <MenuItem Command=”ApplicationCommands.Copy” /> <MenuItem Command=”ApplicationCommands.Cut” /> <MenuItem Command=”ApplicationCommands.Paste” /> </ContextMenu> Secondly, create a style for your TextBoxes, which … Read more

WPF – How to create image button with template

You won’t need dependency properties because you are inheriting from Button. You already have the IsPressed and IsEnabled properties. In fact, this is all you need: <Button x:Class=”TestWpfApplication.ThreeStateImageButton” xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”> <Button.Template> <ControlTemplate TargetType=”{x:Type Button}”> <Grid> <Image Name=”Normal” Source=”Resources/Normal.png”/> <Image Name=”Pressed” Source=”Resources/Pressed.png” Visibility=”Hidden”/> <Image Name=”Disabled” Source=”Resources/Disabled.png” Visibility=”Hidden”/> </Grid> <ControlTemplate.Triggers> <Trigger Property=”IsPressed” Value=”True”> <Setter TargetName=”Normal” Property=”Visibility” Value=”Hidden”/> … Read more

Two-way binding in WPF

Most probably you’re trying to bind to a .net CLR property instead of a WPF dependencyProperty (which provides Change Notification in addition to some other things). For normal CLR property, you’d need to implement INotifyPropertyChanged and force update on the textbox in the event handler for PropertyChanged. So make your object with the property implement … Read more

How to see design-time data-binding in XAML editor (it works in runtime)?

Make sure that you have these definitions at the root tag of your xaml file (in your case the Window tag): xmlns:mc=”http://schemas.openxmlformats.org/markup-compatibility/2006″ xmlns:d=”http://schemas.microsoft.com/expression/blend/2008″ mc:Ignorable=”d” Then, anywhere in the xaml (including the root tag) you can add this: d:DataContext=”{d:DesignInstance myNamespace:MyViewModel, IsDesignTimeCreatable=True}” Now you just need to make sure that you initialize the values in a constructor … Read more

WPF animation: binding to the “To” attribute of storyboard animation

I’ve had similar situations in ControlTemplates where I’ve wanted to bind the “To” attribute to a value (rather than hard-coding it), and I finally found a solution. Quick side note: If you dig around on the web you’ll find examples of people being able to use data binding for the “From” or “To” properties. However, … Read more

WPF: Cancel a user selection in a databound ListBox?

For future stumblers on this question, this page is what ultimately worked for me: http://blog.alner.net/archive/2010/04/25/cancelling-selection-change-in-a-bound-wpf-combo-box.aspx It’s for a combobox, but works for a listbox just fine, since in MVVM you don’t really care what type of control is calling the setter. The glorious secret, as the author mentions, is to actually change the underlying value … Read more

remove red rectangle around combobox

Use this to modify the Validation.ErrorTemplate <ControlTemplate x:Key=”ComboBoxValidationErrorTemplate”> <DockPanel> <Border BorderBrush=”Blue” BorderThickness=”4″> <AdornedElementPlaceholder /> </Border> </DockPanel> </ControlTemplate> And then use it in your ComboBox like <ComboBox Validation.ErrorTemplate=”{StaticResource ComboBoxValidationErrorTemplate}” …> To have no indication of a Validation Error, remove the DockPanel, set Visibility to Collapsed or any other way you like. Almost forgot, probably the easiest … Read more