WPF Attached Events vs Non-Attached Events

The difference is mostly syntactic, both delegate references are being handled by WPF’s EventManager but what the attached events give you is the ability to declare generic functionality without needing to bloat all your classes’ implementation. In the case of a normal routed event, the class provides the interface to be able to at some … Read more

Change the background color of a toggle button when the toggle button is checked

<ToggleButton Content=”toggle”> <ToggleButton.Style> <Style TargetType=”{x:Type ToggleButton}”> <Setter Property=”Template”> <Setter.Value> <ControlTemplate TargetType=”ToggleButton”> <Border BorderBrush=”{TemplateBinding BorderBrush}” Background=”{TemplateBinding Background}”> <ContentPresenter HorizontalAlignment=”Center” VerticalAlignment=”Center”/> </Border> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <Trigger Property=”IsChecked” Value=”True”> <Setter Property=”Background” Value=”Red” /> </Trigger> </Style.Triggers> </Style> </ToggleButton.Style> </ToggleButton> Nearly the same as Klaus ones, but using “TemplateBinding” instead of “TargetName”. With the TemplateBinding, the ControlTemplate use the … Read more

Define a string as a static resource

You can define it as an application resource: <Application x:Class=”xxxxxx” xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml” xmlns:clr=”clr-namespace:System;assembly=mscorlib” StartupUri=”MainWindow.xaml”> <Application.Resources> <clr:String x:Key=”MyConstString”>My string</clr:String> </Application.Resources> </Application>

Windows 8 Image UniformFill centered

I managed to solve my problem, I made the image larger than the container it was placed in and vertical aligned it in the center. <Grid HorizontalAlignment=”Left” Width=”250″ Height=”125″> <Image Source=”/url/to/image.jpg” Stretch=”UniformToFill” Height=”250″ Margin=”0″ VerticalAlignment=”Center”/> </Grid> The overflow of the image was invisible 🙂

Is there a way to comment out XAML that contains comments?

No, there is no way of having nested comments in XAML. You could use the mc:Ignorable attribute on your root element, and any attribute or element prefixed with that value will be ignored E.g: <UserControl … mc:Ignorable=”i”> <!– Ignore Text attribute –> <TextBlock i:Text=”Hello” /> <!– Ignore entire button –> <i:Button> </i:Button> </UserControl> Note that … Read more

Using a StaticResource SolidColorBrush to define the Gradient Stop Colors

Ok, I found the problem: Using Color and not SolidColorBrush.. <Color x:Key=”colorbrushMedium”>#FF5A5A5A</Color> <Color x:Key=”colorbrushDark”>#FF222222</Color> <LinearGradientBrush> <GradientStop Color=”{StaticResource colorbrushMedium}”/> <GradientStop Color=”{StaticResource colorbrushDark}” Offset=”1″/> </LinearGradientBrush> This seems to solve my problem!