WPF set border background in trigger

Common mistake. You have set the Border.Background property directly which will always override the value set by your trigger. (Locally set values have a very high precedence, style has a pretty low precedence.) Instead, you should move your “normal” background into the Style like so: <Border> <Border.Style> <Style TargetType=”Border”> <Setter Property=”Background”> <Setter.Value> <LinearGradientBrush> <LinearGradientBrush.GradientStops> <GradientStop … Read more

SendKeys.Send Method in WPF application

SendKeys is part of the System.Windows.Forms Namespace there is not an equivalent method in Wpf. You can not use the SendKeys.Send with WPF but you can use the SendKeys.SendWait method, if you add System.Windows.Forms to your project references. Your only other option would be to to PInvoke SendInput. Be aware that both of these methods … Read more

Disconnecting an element from any/unspecified parent container in WPF

You may write a helper class with an extension method: public static class RemoveChildHelper { public static void RemoveChild(this DependencyObject parent, UIElement child) { var panel = parent as Panel; if (panel != null) { panel.Children.Remove(child); return; } var decorator = parent as Decorator; if (decorator != null) { if (decorator.Child == child) { decorator.Child … Read more

Is the CallerMemberName attribute in 4.5 “able to be faked”?

Yes, you can, exactly as you could use LINQ and .NET 2, as you said. I use the following in a .NET 4.0 project with the VS2012 compiler with success: namespace System.Runtime.CompilerServices { [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)] public sealed class CallerMemberNameAttribute : Attribute { } } Be very careful that everyone on … Read more

How to use Application.Exit Event in WPF?

It’s quite simple: Add “Exit” property to the application tag <Application x:Class=”WpfApplication4.App” xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml” StartupUri=”MainWindow.xaml” Exit=”Application_Exit”> </Application> and handle it in the “code behind” private void Application_Exit(object sender, ExitEventArgs e) { // Perform tasks at application exit } The Exit event is fired when the application is shutting down or the Windows session is ending. … Read more

WPF MVVM: how to bind GridViewColumn to ViewModel-Collection?

The Columns property is not a dependency property, so you can’t bind it. However, it might be possible to create an attached property that you could bind to a collection in your ViewModel. This attached property would then create the columns for you. UPDATE OK, here’s a basic implementation… Attached properties using System.Collections.Generic; using System.Collections.Specialized; … 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

WPF – Remove focus when clicking outside of a textbox

Rather than adding new control to window, I think you should give your Grid a name and react to the MouseDown event on your window, moving the focus to the Grid itself. Something like this: <Window x:Class=”WpfApplication1.Window1″ xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml” Title=”Window1″ Height=”412″ Width=”569″ MouseDown=”Window_MouseDown” Name=”window1″> <Grid ShowGridLines=”False” Background=”#01FFFFFF” KeyDown=”Grid_KeyDown” Name=”grid1″ Focusable=”True”> <TextBox Width=”120″ Margin=”117,61,0,0″ Name=”textBox1″ VerticalAlignment=”Top” … Read more