passing the current Window as a CommandParameter

There are two ways I can of think to do this: Give the window a name (via a x:Name attribute on the Window tag, and then construct a binding like this (assumes the name of the window is ‘ThisWindow’): <Button Command=”CommandGetsCalled” CommandParameter=”{Binding ElementName=ThisWindow}” /> For something more general (doesn’t rely on giving the current Window … Read more

Assign Short Cut Key to a button in WPF

This is kind of old, but I ran into the same issue today. I found that the simplest solution is to just use an AccessText element for the button’s content. <Button Command=”{Binding SomeCommand}”> <AccessText>_Help</AccessText> </Button> When you press the Alt key, the ‘H’ will be underlined on the button. When you press the key combination … Read more

ItemsControl with multiple DataTemplates for a viewmodel

You can create multiple ObservableCollections and then bind your ItemsSource to a CompositeCollection which joins those collections. Then in your XAML you can create different DataTemplates for the respective types using the DataType property which like styles gets automatically applied if it is placed in the resources. (You can also create the composite in XAML … 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

tech