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

custom combobox in wpf Application

I also liked the format and decided to reproduce it. Please find the XAML below. Hope it helps someone… <Window x:Class=”ComboStyle.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″> <Window.Resources> <ControlTemplate x:Key=”ComboBoxToggleButton” TargetType=”{x:Type ToggleButton}”> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition Width=”20″ /> </Grid.ColumnDefinitions> <Border x:Name=”Border” Grid.ColumnSpan=”2″ CornerRadius=”0″ Background=”#FF3F3F3F” BorderBrush=”#FF97A0A5″ BorderThickness=”1″ /> <Border Grid.Column=”0″ CornerRadius=”0″ Margin=”1″ Background=”#FF3F3F3F” BorderBrush=”#FF97A0A5″ BorderThickness=”0,0,1,0″ /> … Read more

In WPF Stretch a control to fill a ListView Column

After narrowing down my question I was about to Google and find an answer here. Basically for some reason the ListViewItems are set to align to the Left. Setting them to Stretch fixes this problem. This is done through a style like so: <ListView.ItemContainerStyle> <Style TargetType=”ListViewItem”> <Setter Property=”HorizontalContentAlignment” Value=”Stretch” /> </Style> </ListView.ItemContainerStyle> This unfortunately affects … 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

Image not displaying at runtime C# WPF

In your project: Create a folder say “img”, right click on it and select add existing item add image to folder Go to properties of the added image, set Build Action as Resource. It worked for me. In XAML <Image HorizontalAlignment=”Left” Name=”MyImg” Height=”80″ Margin=”273,147,0,0″ VerticalAlignment=”Top” Width=”100″ Source=”/img/Desert.jpg”/>

How to get instance of Panel that holds content of ItemsControl?

It’s a little tricky since you don’t know the name of the Panel so you can’t use FindName etc. This will work for most cases where an ItemsPresenter is present private Panel GetItemsPanel(DependencyObject itemsControl) { ItemsPresenter itemsPresenter = GetVisualChild<ItemsPresenter>(itemsControl); Panel itemsPanel = VisualTreeHelper.GetChild(itemsPresenter, 0) as Panel; return itemsPanel; } An implementation of GetVisualChild private static … Read more

Change selected and unfocused Listbox style to not be grayed out

I have done something like this using the following in a merged ResourceDictionary, it may help you: <Style TargetType=”ListBoxItem”> <Style.Resources> <!–SelectedItem with focus–> <SolidColorBrush x:Key=”{x:Static SystemColors.HighlightBrushKey}” Color=”LightBlue” Opacity=”.4″/> <!–SelectedItem without focus–> <SolidColorBrush x:Key=”{x:Static SystemColors.InactiveSelectionHighlightBrushKey }” Color=”LightBlue” Opacity=”.4″/> </Style.Resources> </Style>

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>