DropShadow for WPF Borderless Window

I have written a little utility class that is able to do exactly what you want: drop a standard shadow over a borderless Window but having AllowsTransparency set to false. You just have to call the DropShadowToWindow(Window window) method. It is preferred that you make this call just after the window’s constructor’s InitializeComponent(), but it … Read more

Is there a way to use data-template inheritance in WPF?

The only thing that I have found do to for this kind of thing is this: <DataTemplate x:Key=”BaseClass”> <!– base class template here –> </DataTemplate> <DataTemplate DataType=”{x:Type app:BaseClass}”> <ContentPresenter Content=”{Binding}” ContentTemplate=”{StaticResource BaseClass}”/> </DataTemplate> <DataTemplate DataType=”{x:Type app:DerivedClass}”> <StackPanel> <ContentPresenter Content=”{Binding}” ContentTemplate=”{StaticResource BaseClass}”/> <!– derived class extra template here –> </StackPanel> </DataTemplate> Basically this creates a “common” … Read more

Defining MenuItem Shortcuts

H.B. was right… I just wanted to add more precisions. Remove the Click event on your MenuItem and associate it with a Command instead. 1 – Add/create your commands: <Window.CommandBindings> <CommandBinding Command=”Open” Executed=”OpenCommandBinding_Executed”/> <CommandBinding Command=”SaveAs” Executed=”SaveAsCommandBinding_Executed”/> </Window.CommandBindings> The commands are refering to the following code: private void OpenCommandBinding_Executed(object sender, ExecutedRoutedEventArgs e) { Open();//Implementation of open … Read more

What are WPF Preview Events?

From Programming WPF – Chris Sells and Ian Griffith With the exception of direct events, WPF defines most routed events in pairs – one tunnelling and the other bubbling. The tunnelling event name always begins with ‘Preview’ and is raised first. This gives parents the chance to see the event before it reaches the child. … Read more

How to open a new window using MVVM Light Toolkit

Passing a message from ViewModel1 to View1 means to use the messaging capabilities in the MVVM Light Toolkit. For example, your ViewModel1 could have a command called ShowView2Command, then it would send a message to display the view. public class ViewModel1 : ViewModelBase { public RelayCommand ShowView2Command { private set; get; } public ViewModel1() : … Read more

Keyboard events in a WPF MVVM application?

To bring an updated answer, the .net 4.0 framework enables you to do this nicely by letting you bind a KeyBinding Command to a command in a viewmodel. So… If you wanted to listen for the Enter key, you’d do something like this: <TextBox AcceptsReturn=”False”> <TextBox.InputBindings> <KeyBinding Key=”Enter” Command=”{Binding SearchCommand}” CommandParameter=”{Binding Path=Text, RelativeSource={RelativeSource AncestorType={x:Type TextBox}}}” … Read more

DataTrigger where value is NOT null?

This is a bit of a cheat but I just set a default style and then overrode it using a DataTrigger if the value is null… <Style> <!– Highlight for Reviewed (Default) –> <Setter Property=”Control.Background” Value=”PaleGreen” /> <Style.Triggers> <!– Highlight for Not Reviewed –> <DataTrigger Binding=”{Binding Path=REVIEWEDBY}” Value=”{x:Null}”> <Setter Property=”Control.Background” Value=”LightIndianRed” /> </DataTrigger> </Style.Triggers> </Style>

How to change disabled background color of TextBox in WPF

Unfortunately for the TextBox control, it appears like it’s not as simple as just adding a trigger and changing the Background color when the trigger condition is true. You have to override the entire ControlTemplate to achieve this. Below is one example on how you might do this: <Window x:Class=”StackOverflow.MainWindow” xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml” Title=”MainWindow” Height=”350″ Width=”525″> … Read more