How to set a ComboBox header using XAML in WPF - wpf

I've spent the last few days trying to create a header for my comboBox but I can't seem find a way. All I've found so far couldn't be implemented in my model. I have to say that I have a slim understanding on databinding which I have been doing some serious reading on.
I created a comboBox and filled it with data from different databases. Then, I used a converter to concatenate relevant data to be displayed. Now I would like to add a header describing the name of each section.
Here is my XAML:
<Window.Resources>
<local:ConcatenateFieldsMultiValueConverter x:Key="converter"/>
</Window.Resources>
<ComboBox Name="PersonComboBox" HorizontalAlignment="Left" Margin="104,64,0,0" VerticalAlignment="Top" Width="465">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox x:Name="ComboBoxCheckBox" IsChecked="{Binding IsSelected, Mode=TwoWay}"/>
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource converter}" ConverterParameter=", ">
<Binding Path="FirstName" />
<Binding Path="LastName"/>
<Binding Path="Age"/>
<Binding Path="Country"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Here is where I bound to the comboBox:
// Binds the comboBox to the personDataItems list
this.PersonComboBox.ItemsSource = personDataItems;
This is how the header should look:
I want to be able to do it using XAML. Thanks so much for your help.

Xaml code
<Window x:Class="Multiple_Colum_Cmb.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Multiple_Colum_Cmb"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:ComboboxData x:Key="ComboboxData"></local:ComboboxData>
<!--datatemplate-->
<DataTemplate x:Key="Datatemplate">
<Grid SnapsToDevicePixels="True" Margin="0,0,30,0">
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Firstname}" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="0" FontSize="15" TextTrimming="CharacterEllipsis" Foreground="Black" />
<TextBlock Text="{Binding LastName}" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="1" FontSize="15" TextTrimming="CharacterEllipsis" Foreground="Black" />
<TextBlock Text="{Binding Age}" Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="20" Foreground="Black"/>
<TextBlock Text="{Binding Country}" Grid.Column="3" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="20" Foreground="Black"/>
</Grid>
</DataTemplate>
<!--header text-->
<TextBlock x:Key="header1" Text="FirstName"></TextBlock>
<TextBlock x:Key="header2" Text="LastName"></TextBlock>
<TextBlock x:Key="header3" Text="Age"></TextBlock>
<TextBlock x:Key="header4" Text="Country"></TextBlock>
<!--Toggle Button Template-->
<ControlTemplate x:Key="ComboBoxToggleButton" TargetType="ToggleButton">
<Grid x:Name="gd">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="30" />
</Grid.ColumnDefinitions>
<ContentPresenter Grid.Column="0"></ContentPresenter>
<Grid Grid.Column="0">
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding Source={StaticResource header1},Path=Text}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="Black" FontFamily="Segoe Ui Dark" FontSize="15" ></TextBlock>
<TextBlock Grid.Column="1" Text="{Binding Source={StaticResource header2},Path=Text}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="Black" FontFamily="Segoe Ui Dark" FontSize="15"></TextBlock>
<TextBlock Grid.Column="2" Text="{Binding Source={StaticResource header3},Path=Text}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="Black" FontFamily="Segoe Ui Dark" FontSize="15"></TextBlock>
<TextBlock Grid.Column="3" Text="{Binding Source={StaticResource header4},Path=Text}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="Black" FontFamily="Segoe Ui Dark" FontSize="15"></TextBlock>
</Grid>
<Border x:Name="Border" SnapsToDevicePixels="True" Grid.ColumnSpan="2" Background="Transparent" BorderBrush="LightGray" BorderThickness="1"/>
<Border x:Name="Boredr1" SnapsToDevicePixels="True" Grid.Column="0" Margin="1" Background="Transparent" BorderBrush="LightGray" BorderThickness="0,0,1,0" />
<Path x:Name="Arrow" SnapsToDevicePixels="True" Grid.Column="1" Fill="Black" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 0 L 6 6 L 12 0 Z"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="ToggleButton.IsMouseOver" Value="True">
<Setter TargetName="Border" Property="BorderBrush" Value="Black"/>
<Setter TargetName="Boredr1" Property="BorderBrush" Value="Black"/>
</Trigger>
<Trigger Property="ToggleButton.IsChecked" Value="True">
<Setter TargetName="Arrow" Property="Data" Value="M 0 0 L 5 5 L 10 0"/>
<Setter TargetName="Arrow" Property="Fill" Value="White"/>
<Setter TargetName="Arrow" Property="Stroke" Value="Black"/>
<Setter TargetName="Arrow" Property="StrokeThickness" Value="1.5"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="gd" Property="Visibility" Value="Visible"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<!--TextBox Template-->
<ControlTemplate x:Key="ComboBoxTextBox" TargetType="TextBox">
<ScrollViewer x:Name="PART_ContentHost" Focusable="False" />
</ControlTemplate>
<!--Multiple column combobox-->
<Style x:Key="Multiple_Column_Cmb" TargetType="ComboBox">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Disabled"/>
<Setter Property="Height" Value="40"/>
<Setter Property="MinWidth" Value="120"/>
<Setter Property="MinHeight" Value="20"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBox">
<Grid>
<ToggleButton Name="ToggleButton" Foreground="Black" Template="{StaticResource ComboBoxToggleButton}" Focusable="false" IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press">
</ToggleButton>
<ContentPresenter Name="ContentSite" IsHitTestVisible="False" Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Margin="3,3,23,3" VerticalAlignment="Center" HorizontalAlignment="Left" />
<TextBox OverridesDefaultStyle="True" SelectionBrush="Gray" CaretBrush="Black" Margin="0,0,30,0" TextWrapping="NoWrap" x:Name="PART_EditableTextBox" FontFamily="Segoe UI Dark" Foreground="Black" Style="{x:Null}" Template="{StaticResource ComboBoxTextBox}" HorizontalAlignment="Left" Focusable="True" VerticalAlignment="Center" FontSize="15" Visibility="Hidden" IsReadOnly="{TemplateBinding IsReadOnly}"/>
<Popup Name="Popup" Grid.ColumnSpan="2" Placement="Bottom" IsOpen="{TemplateBinding IsDropDownOpen}" AllowsTransparency="True" Focusable="False" PopupAnimation="Slide">
<ContentControl Name="DropDown" SnapsToDevicePixels="True" MaxWidth="{TemplateBinding ActualWidth}" MinWidth="{TemplateBinding ActualWidth}" MaxHeight="{TemplateBinding MaxDropDownHeight}">
<Grid Background="White" SnapsToDevicePixels="True" MaxWidth="{TemplateBinding ActualWidth}" MinWidth="{TemplateBinding ActualWidth}" MaxHeight="{TemplateBinding MaxDropDownHeight}">
<Border x:Name="DropDownBorder" SnapsToDevicePixels="True" Background="Transparent" MaxWidth="{TemplateBinding ActualWidth}" BorderThickness="1" BorderBrush="LightGray"/>
<ScrollViewer ScrollViewer.CanContentScroll="False" Grid.Row="1" Margin="0,0,0,0" SnapsToDevicePixels="True">
<StackPanel IsItemsHost="True" Background="Transparent">
</StackPanel>
</ScrollViewer>
</Grid>
</ContentControl>
</Popup>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsEditable" Value="true">
<Setter Property="IsTabStop" Value="false"/>
<Setter TargetName="PART_EditableTextBox" Property="Background" Value="White"/>
<Setter TargetName="PART_EditableTextBox" Property="Visibility" Value="Visible"/>
<Setter TargetName="PART_EditableTextBox" Property="Foreground" Value="Black"/>
<Setter TargetName="ContentSite" Property="Visibility" Value="Hidden"/>
</Trigger>
<Trigger Property="HasItems" Value="false">
<Setter TargetName="DropDownBorder" Property="MinHeight" Value="95"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="IsTabStop" Value="false"/>
<Setter TargetName="PART_EditableTextBox" Property="Visibility" Value="Visible"/>
<Setter TargetName="PART_EditableTextBox" Property="Foreground" Value="Black"/>
<Setter TargetName="PART_EditableTextBox" Property="IsEnabled" Value="False"/>
<Setter TargetName="PART_EditableTextBox" Property="Background" Value="White"/>
<Setter TargetName="ContentSite" Property="Visibility" Value="Hidden"/>
</Trigger>
<Trigger Property="IsGrouping" Value="true">
<Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
</Trigger>
<Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="true">
<Setter TargetName="DropDownBorder" Property="Margin" Value="0,2,0,0"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
</Style.Triggers>
</Style>
<!--combobox item style-->
<Style x:Key="Column_CmbItem" TargetType="ComboBoxItem">
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBoxItem">
<Grid x:Name="Border" Grid.ColumnSpan="2" Margin="1,0,1,1" Background="White">
<ContentPresenter></ContentPresenter>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="ComboBoxItem.IsSelected" Value="True">
<Setter TargetName="Border" Property="Background" Value="Gray"></Setter>
</Trigger>
<Trigger Property="ComboBoxItem.IsMouseOver" Value="True">
<Setter TargetName="Border" Property="Background" Value="LightBlue"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<ComboBox x:Name="Combobox1" IsReadOnly="True" ItemTemplate="{StaticResource Datatemplate }" Margin="0,0,50,0" HorizontalAlignment="Right" IsEditable="False" ItemsSource="{Binding Source={StaticResource ComboboxData}}" Style="{StaticResource Multiple_Column_Cmb}" Height="30" ItemContainerStyle="{StaticResource Column_CmbItem}" Width="400"/>
</Window>
C# code
InitializeComponent();
List<ComboboxData> cmb = new List<ComboboxData>();
cmb.Add(new ComboboxData("oliver", "stone", "30", "USA"));
cmb.Add(new ComboboxData("Joseph", "Truan", "35", "Canada"));
Combobox1.ItemsSource = cmb;
public class ComboboxData
{
public string Firstname { get; set; }
public string LastName { get; set; }
public string Age { get; set; }
public string Country { get; set; }
public ComboboxData(string Firstname, string LastName, string Age, string Country)
{
this.Firstname = Firstname;
this.LastName = LastName;
this.Age = Age;
this.Country = Country;
}
public ComboboxData() { }
}
Output
Update :
I have designed two tempale for togglebutton
1) When combobox item is not selected ie.selectdeindex=-1 (NoItemselected)
2) Default template(ShowselectedItem)
<!--header text-->
<TextBlock x:Key="header1" Text="FirstName"></TextBlock>
<TextBlock x:Key="header2" Text="LastName"></TextBlock>
<TextBlock x:Key="header3" Text="Age"></TextBlock>
<TextBlock x:Key="header4" Text="Country"></TextBlock>
<!--Toggle Button Template1-->
<ControlTemplate x:Key="NoItemselected" TargetType="ToggleButton">
<Grid x:Name="gd">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="30" />
</Grid.ColumnDefinitions>
<ContentPresenter Grid.Column="0"></ContentPresenter>
<Grid Grid.Column="0">
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding Source={StaticResource header1},Path=Text}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="Black" FontFamily="Segoe Ui Dark" FontSize="15" ></TextBlock>
<TextBlock Grid.Column="1" Text="{Binding Source={StaticResource header2},Path=Text}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="Black" FontFamily="Segoe Ui Dark" FontSize="15"></TextBlock>
<TextBlock Grid.Column="2" Text="{Binding Source={StaticResource header3},Path=Text}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="Black" FontFamily="Segoe Ui Dark" FontSize="15"></TextBlock>
<TextBlock Grid.Column="3" Text="{Binding Source={StaticResource header4},Path=Text}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="Black" FontFamily="Segoe Ui Dark" FontSize="15"></TextBlock>
</Grid>
<Border x:Name="Border" SnapsToDevicePixels="True" Grid.ColumnSpan="2" Background="Transparent" BorderBrush="LightGray" BorderThickness="1"/>
<Border x:Name="Boredr1" SnapsToDevicePixels="True" Grid.Column="0" Margin="1" Background="Transparent" BorderBrush="LightGray" BorderThickness="0,0,1,0" />
<Path x:Name="Arrow" SnapsToDevicePixels="True" Grid.Column="1" Fill="Black" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 0 L 6 6 L 12 0 Z"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="ToggleButton.IsMouseOver" Value="True">
<Setter TargetName="Border" Property="BorderBrush" Value="Black"/>
<Setter TargetName="Boredr1" Property="BorderBrush" Value="Black"/>
</Trigger>
<Trigger Property="ToggleButton.IsChecked" Value="True">
<Setter TargetName="Arrow" Property="Data" Value="M 0 0 L 5 5 L 10 0"/>
<Setter TargetName="Arrow" Property="Fill" Value="White"/>
<Setter TargetName="Arrow" Property="Stroke" Value="Black"/>
<Setter TargetName="Arrow" Property="StrokeThickness" Value="1.5"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="gd" Property="Visibility" Value="Visible"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<!--Toggle Button Template2-->
<ControlTemplate x:Key="ShowselectedItem" TargetType="ToggleButton">
<Grid x:Name="gd">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="30" />
</Grid.ColumnDefinitions>
<ContentPresenter Grid.Column="0"></ContentPresenter>
<Border x:Name="Border" SnapsToDevicePixels="True" Grid.ColumnSpan="2" Background="Transparent" BorderBrush="LightGray" BorderThickness="1"/>
<Border x:Name="Boredr1" SnapsToDevicePixels="True" Grid.Column="0" Margin="1" Background="Transparent" BorderBrush="LightGray" BorderThickness="0,0,1,0" />
<Path x:Name="Arrow" SnapsToDevicePixels="True" Grid.Column="1" Fill="Black" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 0 L 6 6 L 12 0 Z"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="ToggleButton.IsMouseOver" Value="True">
<Setter TargetName="Border" Property="BorderBrush" Value="Black"/>
<Setter TargetName="Boredr1" Property="BorderBrush" Value="Black"/>
</Trigger>
<Trigger Property="ToggleButton.IsChecked" Value="True">
<Setter TargetName="Arrow" Property="Data" Value="M 0 0 L 5 5 L 10 0"/>
<Setter TargetName="Arrow" Property="Fill" Value="White"/>
<Setter TargetName="Arrow" Property="Stroke" Value="Black"/>
<Setter TargetName="Arrow" Property="StrokeThickness" Value="1.5"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="gd" Property="Visibility" Value="Visible"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<!--TextBox Template-->
<ControlTemplate x:Key="ComboBoxTextBox" TargetType="TextBox">
<ScrollViewer x:Name="PART_ContentHost" Focusable="False" />
</ControlTemplate>
<!--Multiple column combobox-->
<Style x:Key="Multiple_Column_Cmb" TargetType="ComboBox">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Disabled"/>
<Setter Property="Height" Value="40"/>
<Setter Property="MinWidth" Value="120"/>
<Setter Property="MinHeight" Value="20"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBox">
<Grid>
<ToggleButton Name="ToggleButton" Template="{StaticResource ShowselectedItem}" Focusable="false" IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press"/>
<ContentPresenter Name="ContentSite" IsHitTestVisible="False" Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Margin="3,3,23,3" VerticalAlignment="Center" HorizontalAlignment="Left" />
<TextBox OverridesDefaultStyle="True" SelectionBrush="Gray" CaretBrush="Black" Margin="0,0,30,0" TextWrapping="NoWrap" x:Name="PART_EditableTextBox" FontFamily="Segoe UI Dark" Foreground="Black" Style="{x:Null}" Template="{StaticResource ComboBoxTextBox}" HorizontalAlignment="Left" Focusable="True" VerticalAlignment="Center" FontSize="15" Visibility="Hidden" IsReadOnly="{TemplateBinding IsReadOnly}"/>
<Popup Name="Popup" Grid.ColumnSpan="2" Placement="Bottom" IsOpen="{TemplateBinding IsDropDownOpen}" AllowsTransparency="True" Focusable="False" PopupAnimation="Slide">
<ContentControl Name="DropDown" SnapsToDevicePixels="True" MaxWidth="{TemplateBinding ActualWidth}" MinWidth="{TemplateBinding ActualWidth}" MaxHeight="{TemplateBinding MaxDropDownHeight}">
<Grid Background="White" SnapsToDevicePixels="True" MaxWidth="{TemplateBinding ActualWidth}" MinWidth="{TemplateBinding ActualWidth}" MaxHeight="{TemplateBinding MaxDropDownHeight}">
<Border x:Name="DropDownBorder" SnapsToDevicePixels="True" Background="Transparent" MaxWidth="{TemplateBinding ActualWidth}" BorderThickness="1" BorderBrush="LightGray"/>
<ScrollViewer ScrollViewer.CanContentScroll="False" Grid.Row="1" Margin="0,0,0,0" SnapsToDevicePixels="True">
<StackPanel IsItemsHost="True" Background="Transparent">
</StackPanel>
</ScrollViewer>
</Grid>
</ContentControl>
</Popup>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsEditable" Value="true">
<Setter Property="IsTabStop" Value="false"/>
<Setter TargetName="PART_EditableTextBox" Property="Background" Value="White"/>
<Setter TargetName="PART_EditableTextBox" Property="Visibility" Value="Visible"/>
<Setter TargetName="PART_EditableTextBox" Property="Foreground" Value="Black"/>
<Setter TargetName="ContentSite" Property="Visibility" Value="Hidden"/>
</Trigger>
<Trigger Property="HasItems" Value="false">
<Setter TargetName="DropDownBorder" Property="MinHeight" Value="95"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="IsTabStop" Value="false"/>
<Setter TargetName="PART_EditableTextBox" Property="Visibility" Value="Visible"/>
<Setter TargetName="PART_EditableTextBox" Property="Foreground" Value="Black"/>
<Setter TargetName="PART_EditableTextBox" Property="IsEnabled" Value="False"/>
<Setter TargetName="PART_EditableTextBox" Property="Background" Value="White"/>
<Setter TargetName="ContentSite" Property="Visibility" Value="Hidden"/>
</Trigger>
<Trigger Property="IsGrouping" Value="true">
<Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
</Trigger>
<Trigger Property="SelectedIndex" Value="-1">
<Setter Property="Template" TargetName="ToggleButton" Value="{StaticResource NoItemselected}"></Setter>
</Trigger>
<Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="true">
<Setter TargetName="DropDownBorder" Property="Margin" Value="0,2,0,0"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

Related

Plot line graph on button Click

I am developing wpf application.In that I need to plot a real time graph on clicking a button.
I tried writing this.MyModel.InvalidatePlot(true); in click().
Here is the code which I tried.
MainWindow.xaml
<Window x:Class="OxyPlotSample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:oxy="http://oxyplot.org/wpf"
mc:Ignorable="d"
Title="FX2 Controller Communication" Height="768" Width="1366" WindowStartupLocation="CenterScreen" WindowState="Maximized">
<Window.Resources>
<Style x:Key="menuParentStyle" TargetType="MenuItem">
<Setter Property="Background" Value="Silver"/>
<Setter Property="Foreground" Value="Black"/>
</Style>
<SolidColorBrush x:Key="Menu.Static.Foreground" Color="Black"/>
<SolidColorBrush x:Key="Menu.Static.Background" Color="Silver"/>
<SolidColorBrush x:Key="Menu.Static.Border" Color="Transparent"/>
<Style x:Key="MenuItemStyle1" TargetType="{x:Type MenuItem}">
<Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="Background" Value="Silver"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="BorderThickness" Value="2"/>
<Setter Property="ScrollViewer.PanningMode" Value="Both"/>
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
<Setter Property="Template" Value="{DynamicResource MenuItemControlTemplate1}"/>
<Style.Triggers>
<Trigger Property="Role" Value="TopLevelHeader">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Foreground" Value="{StaticResource Menu.Static.Foreground}"/>
<Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=TopLevelHeaderTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}"/>
<Setter Property="Padding" Value="6,0"/>
</Trigger>
<Trigger Property="Role" Value="TopLevelItem">
<Setter Property="Background" Value="{StaticResource Menu.Static.Background}"/>
<Setter Property="BorderBrush" Value="{StaticResource Menu.Static.Border}"/>
<Setter Property="Foreground" Value="{StaticResource Menu.Static.Foreground}"/>
<Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=TopLevelItemTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}"/>
<Setter Property="Padding" Value="6,0"/>
</Trigger>
<Trigger Property="Role" Value="SubmenuHeader">
<Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=SubmenuHeaderTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="Pink" />
</Trigger>
<Trigger Property="IsKeyboardFocusWithin" Value="true">
<Setter Property="Background" Value="#0a99f3" />
</Trigger>
</Style.Triggers>
</Style>
<ControlTemplate x:Key="MenuItemControlTemplate1" TargetType="{x:Type MenuItem}">
<Border x:Name="templateRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
<Grid Margin="-1">
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="22" SharedSizeGroup="MenuItemIconColumnGroup" Width="Auto"/>
<ColumnDefinition Width="13"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="30"/>
<ColumnDefinition SharedSizeGroup="MenuItemIGTColumnGroup" Width="Auto"/>
<ColumnDefinition Width="20"/>
</Grid.ColumnDefinitions>
<ContentPresenter x:Name="Icon" Content="{TemplateBinding Icon}" ContentSource="Icon" HorizontalAlignment="Center" Height="16" Margin="3" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" Width="16"/>
<Border x:Name="GlyphPanel" BorderBrush="#FF26A0DA" BorderThickness="1" Background="#3D26A0DA" ClipToBounds="False" HorizontalAlignment="Center" Height="22" Margin="-1,0,0,0" Visibility="Hidden" VerticalAlignment="Center" Width="22">
<Path x:Name="Glyph" Data="F1M10,1.2L4.7,9.1 4.5,9.1 0,5.2 1.3,3.5 4.3,6.1 8.3,0 10,1.2z" Fill="#FF212121" FlowDirection="LeftToRight" Height="11" Width="10"/>
</Border>
<ContentPresenter x:Name="menuHeaderContainer" ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" Grid.Column="2" ContentStringFormat="{TemplateBinding HeaderStringFormat}" ContentSource="Header" HorizontalAlignment="Left" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center"/>
<TextBlock x:Name="menuGestureText" Grid.Column="4" Margin="{TemplateBinding Padding}" Opacity="0.7" Text="{TemplateBinding InputGestureText}" VerticalAlignment="Center"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="Icon" Value="{x:Null}">
<Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
</Trigger>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Visibility" TargetName="GlyphPanel" Value="Visible"/>
<Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
</Trigger>
<Trigger Property="IsHighlighted" Value="True">
<Setter Property="Background" TargetName="templateRoot" Value="Blue"/>
<!--<Setter Property="BorderBrush" TargetName="templateRoot" Value="Yellow"/>-->
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="TextElement.Foreground" TargetName="templateRoot" Value="#FF707070"/>
<Setter Property="Fill" TargetName="Glyph" Value="#FF707070"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsHighlighted" Value="True"/>
<Condition Property="IsEnabled" Value="False"/>
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="templateRoot" Value="#0A000000"/>
<Setter Property="BorderBrush" TargetName="templateRoot" Value="#21000000"/>
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<ControlTemplate x:Key="MenuItemControlTemplate2" TargetType="{x:Type MenuItem}">
<Border x:Name="templateRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
<Grid VerticalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<ContentPresenter x:Name="Icon" Content="{TemplateBinding Icon}" ContentSource="Icon" HorizontalAlignment="Center" Height="16" Margin="3" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" Width="16"/>
<Path x:Name="GlyphPanel" Data="F1M10,1.2L4.7,9.1 4.5,9.1 0,5.2 1.3,3.5 4.3,6.1 8.3,0 10,1.2z" Fill="{TemplateBinding Foreground}" FlowDirection="LeftToRight" Margin="3" Visibility="Collapsed" VerticalAlignment="Center"/>
<ContentPresenter ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" Grid.Column="1" ContentStringFormat="{TemplateBinding HeaderStringFormat}" ContentSource="Header" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
<Popup x:Name="PART_Popup" AllowsTransparency="True" Focusable="False" IsOpen="{Binding IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}" Placement="Bottom">
<Border x:Name="SubMenuBorder" BorderBrush="#FF999999" BorderThickness="1" Background="#FFF0F0F0" Padding="2">
<ScrollViewer x:Name="SubMenuScrollViewer" Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}">
<Grid RenderOptions.ClearTypeHint="Enabled">
<Canvas HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0">
<Rectangle x:Name="OpaqueRect" Fill="{Binding Background, ElementName=SubMenuBorder}" Height="{Binding ActualHeight, ElementName=SubMenuBorder}" Width="{Binding ActualWidth, ElementName=SubMenuBorder}"/>
</Canvas>
<Rectangle Fill="#FFD7D7D7" HorizontalAlignment="Left" Margin="29,2,0,2" Width="1"/>
<ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Cycle" Grid.IsSharedSizeScope="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" KeyboardNavigation.TabNavigation="Cycle"/>
</Grid>
</ScrollViewer>
</Border>
</Popup>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSuspendingPopupAnimation" Value="True">
<Setter Property="PopupAnimation" TargetName="PART_Popup" Value="None"/>
</Trigger>
<Trigger Property="Icon" Value="{x:Null}">
<Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
</Trigger>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Visibility" TargetName="GlyphPanel" Value="Visible"/>
<Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
</Trigger>
<Trigger Property="IsHighlighted" Value="True">
<Setter Property="Background" TargetName="templateRoot" Value="Blue"/>
<!--<Setter Property="BorderBrush" TargetName="templateRoot" Value="#FF26A0DA"/>-->
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="TextElement.Foreground" TargetName="templateRoot" Value="#FF707070"/>
<Setter Property="Fill" TargetName="GlyphPanel" Value="#FF707070"/>
</Trigger>
<Trigger Property="CanContentScroll" SourceName="SubMenuScrollViewer" Value="False">
<Setter Property="Canvas.Top" TargetName="OpaqueRect" Value="{Binding VerticalOffset, ElementName=SubMenuScrollViewer}"/>
<Setter Property="Canvas.Left" TargetName="OpaqueRect" Value="{Binding HorizontalOffset, ElementName=SubMenuScrollViewer}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Window.Resources>
<Grid>
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="_File" Style="{StaticResource menuParentStyle}" ItemContainerStyle="{DynamicResource MenuItemStyle1}" Template="{DynamicResource MenuItemControlTemplate2}" Height="30" FontSize="14">
<MenuItem Command="Save" Height="30" FontSize="14"/>
<Separator />
<MenuItem Header="_Exit" Height="30" FontSize="14"/>
</MenuItem>
<MenuItem Header="_Aquire Mode" Style="{StaticResource menuParentStyle}" ItemContainerStyle="{DynamicResource MenuItemStyle1}" Template="{DynamicResource MenuItemControlTemplate2}" Height="30" FontSize="14">
<MenuItem Header="Sum" Height="30" FontSize="14"/>
<MenuItem Header="Average" Height="30" FontSize="14"/>
</MenuItem>
</Menu>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200" />
<ColumnDefinition Width="5" />
</Grid.ColumnDefinitions>
<Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="685" Margin="9,11,0,0" Stroke="Black" VerticalAlignment="Top" Width="181"/>
<StatusBar HorizontalAlignment="Left" Height="70" Margin="5,622,-1143,0" VerticalAlignment="Top" Width="1144" Grid.Column="1">
<ScrollBar Height="46" Width="12"/>
</StatusBar>
<Button Content="Single" HorizontalAlignment="Left" Margin="44,352,0,0" VerticalAlignment="Top" Width="132" Height="59" FontSize="20" Click="Single_Click"/>
<Button Content="Continuous" HorizontalAlignment="Left" Margin="41,468,0,0" VerticalAlignment="Top" Width="130" Height="68" FontSize="20"/>
<Button Content="Stop" HorizontalAlignment="Left" Margin="43,573,0,0" VerticalAlignment="Top" Width="126" Height="64" FontSize="20"/>
<TextBox HorizontalAlignment="Left" Height="36" Margin="45,133,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120"/>
<TextBox HorizontalAlignment="Left" Height="36" Margin="47,257,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120"/>
<Label Content="Accumulation Number :" HorizontalAlignment="Left" Margin="36,65,0,0" VerticalAlignment="Top" Width="146" Height="34" FontSize="13"/>
<Label Content="Integration Time :" HorizontalAlignment="Left" Margin="48,197,0,0" VerticalAlignment="Top" Width="123" Height="34" FontSize="14"/>
<GridSplitter Grid.Column="1" Width="5" HorizontalAlignment="Stretch" />
</Grid>
<Grid>
<oxy:PlotView x:Name="Plot1" Model="{Binding MyModel}" Margin="10,10,10,98" DataContext="MyModel.Series" />
<oxy:LineSeries DataContext="MyModel.Series"/>
</Grid>
</DockPanel>
</Grid>
</Window>
MainWindow.xaml.cs
using System.Collections.Generic;
using System.Windows;
using OxyPlot;
namespace OxyPlotSample
{
public partial class MainWindow : Window
{
public MainWindow()
{
this.MyModel = plot;
this.DataContext = this;
InitializeComponent();
}
public PlotModel MyModel { get; set; }
public PlotModel plot = new PlotModel { };
private void Single_Click(object sender, RoutedEventArgs e)
{
System.Diagnostics.Debug.WriteLine("Single Clicked!!!!");
OxyPlot.Series.LineSeries series = new OxyPlot.Series.LineSeries();
List<double> XValues = new List<double> { 0, 5, 10, 22, 30 };
List<double> YValues = new List<double> { 2, 11, 4, 15, 20 };
this.MyModel = new PlotModel { Title = "Example 1" };
System.Diagnostics.Debug.WriteLine(XValues.Count);
for (int i = 0; i < XValues.Count; i++)
{
System.Diagnostics.Debug.WriteLine(XValues[i] + "," + YValues[i]);
series.Points.Add(new DataPoint(XValues[i], YValues[i]));
}
plot.Series.Add(series);
this.MyModel = plot;
this.DataContext = this;
this.MyModel.InvalidatePlot(true);
System.Diagnostics.Debug.WriteLine(MyModel.Series.Count);
}
}
}
I am expecting to update the datacontext after adding series. Please Suggest the solution for this.
You are mixing too many ideas here. If you are using PlotView and also have the Model defined with the required series, all you would need to do is to Bind the model.
<Grid>
<oxy:PlotView x:Name="Plot1" Model="{Binding MyModel}" Margin="8,11,12,97" />
</Grid>
Since your Model already has the details for the plotting the Line Series, It would plot the Line Series by itself.

WPF Combobox selection is not clearing when text box inside custom combobox is focused

I have a custom WPF combobox, on top I have textbox and below item template which will display the list of items. Now when I move mouse over the items then the respecting is getting selected, but without clicking on any item just clicking into textbox which is present inside same combobox. Now I want the selection from combobox to be cleared. Because when I type something in textbox and click on enter key then it is considering the selected item as last highlighted one. But actually if I click on textbox and typed something then i want to do different process.
<Style x:Key="MyCustomComboBox" TargetType="{x:Type ComboBox}">
<Setter Property="FocusVisualStyle" Value="{StaticResource ComboBoxFocusVisual}" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Background" Value="{StaticResource myWhite}" />
<Setter Property="BorderBrush" Value="{StaticResource myBlue}" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
<Setter Property="Padding" Value="16,5,10,0" />
<Setter Property="ScrollViewer.CanContentScroll" Value="true" />
<Setter Property="ScrollViewer.PanningMode" Value="Both" />
<Setter Property="Stylus.IsFlicksEnabled" Value="False" />
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<UniformGrid Columns="2" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBox}">
<Grid x:Name="MainGrid" SnapsToDevicePixels="true">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="0" MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" />
</Grid.ColumnDefinitions>
<Popup x:Name="PART_Popup"
Grid.ColumnSpan="2"
Margin="1"
AllowsTransparency="true"
IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}"
Placement="Bottom"
PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}">
<Themes:SystemDropShadowChrome x:Name="Shdw"
Width="Auto"
MaxHeight="{TemplateBinding MaxDropDownHeight}"
CornerRadius="5"
Color="Transparent">
<Border x:Name="DropDownBorder"
Padding="0,1,0,0"
Background="White"
BorderBrush="#92AEC4"
BorderThickness="1"
CornerRadius="5">
<Grid Width="300" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<Grid Grid.Row="0"
Width="250"
Margin="8,4,8,16">
<Border Height="24"
BorderBrush="#656565"
BorderThickness="1"
CornerRadius="3">
<StackPanel Margin="8,0" Orientation="Horizontal">
<TextBlock Padding="0,0,5,0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontFamily="ACE-Icon-Font"
FontSize="15"
Foreground="#656565"
Text="" />
<Grid>
<TextBox x:Name="SearchTextBox"
Width="{Binding ElementName=DropDownBorder, Path=ActualWidth}"
VerticalAlignment="Center"
Foreground="#333333"
IsEnabled="True"
Focusable="True"
Text="{Binding Path=CustNameSearch, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
<TextBox.Style>
<Style TargetType="TextBox">
<Setter Property="BorderThickness" Value="0" />
</Style>
</TextBox.Style>
</TextBox>
</Grid>
</StackPanel>
</Border>
</Grid>
<Grid Grid.Row="1"
Background="Transparent"
RenderOptions.ClearTypeHint="Enabled">
<ScrollViewer x:Name="DropDownScrollViewer"
MaxHeight="194"
Template="{DynamicResource ScrollViewerControlTemplate1}">
<Grid>
<Canvas Width="0"
Height="0"
HorizontalAlignment="Left"
VerticalAlignment="Top">
<Rectangle x:Name="OpaqueRect"
Grid.Column="1"
Width="{Binding ActualWidth, ElementName=DropDownBorder}"
Height="{Binding ActualHeight, ElementName=DropDownScrollViewer}"
Fill="{Binding Background, ElementName=DropDownBorder}" />
</Canvas>
<ItemsPresenter x:Name="ItemsPresenter"
KeyboardNavigation.DirectionalNavigation="Contained"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
<Button Margin="16,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Command="{Binding Path=HandleCommandsCommand}"
CommandParameter="AddCustDetails"
Content="Create Customer"
Visibility="{Binding Path=CreateCustButtonVisibility, FallbackValue=Collapsed}">
</Button>
</Grid>
</ScrollViewer>
</Grid>
</Grid>
</Border>
</Themes:SystemDropShadowChrome>
</Popup>
<ToggleButton x:Name="TogB"
Grid.ColumnSpan="2"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
Style="{StaticResource ComboBoxReadonlyToggleButton}" />
<ContentPresenter x:Name="ConPres"
Margin="{TemplateBinding Padding}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Content="Select Cust.."
ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}"
ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
IsHitTestVisible="false"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
<ContentPresenter.Style>
<Style>
<Setter Property="TextBlock.Foreground" Value="White" />
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=TogB, Path=IsChecked}" Value="True">
<Setter Property="TextBlock.Foreground" Value="#FFFFFF" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentPresenter.Style>
</ContentPresenter>
</Grid>
<ControlTemplate.Triggers>
<Trigger SourceName="PART_Popup" Property="HasDropShadow" Value="true">
<Setter TargetName="Shdw" Property="Margin" Value="0,0,3,3" />
<Setter TargetName="Shdw" Property="Color" Value="#16000000" />
</Trigger>
<Trigger Property="HasItems" Value="false">
<Setter TargetName="DropDownBorder" Property="Height" Value="120" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter TargetName="ConPres" Property="TextBlock.Foreground" Value="#CBCBCB" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsGrouping" Value="true" />
<Condition Property="VirtualizingPanel.IsVirtualizingWhenGrouping" Value="false" />
</MultiTrigger.Conditions>
<Setter Property="ScrollViewer.CanContentScroll" Value="false" />
</MultiTrigger>
<Trigger SourceName="DropDownScrollViewer" Property="ScrollViewer.CanContentScroll" Value="false">
<Setter TargetName="OpaqueRect" Property="Canvas.Top" Value="{Binding VerticalOffset, ElementName=DropDownScrollViewer}" />
<Setter TargetName="OpaqueRect" Property="Canvas.Left" Value="{Binding HorizontalOffset, ElementName=DropDownScrollViewer}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsEditable" Value="true">
<Setter Property="BorderBrush" Value="{StaticResource TextBoxBorder}" />
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" />
<Setter Property="IsTabStop" Value="false" />
<Setter Property="Padding" Value="3" />
<Setter Property="Template" Value="{StaticResource ComboBoxEditableTemplate}" />
</Trigger>
</Style.Triggers>
</Style>
If user click on textbox inside to combobox then the selection from combobox should clear. Or EnterKey pressed on textbox then it should not select anything form combobox and should remain same.

How to implement AutoSize Grid Layout in Treeview

I want to implement a grid layout for a treeview, where the most right column has a *-size and the most left column has always the size.
Xaml
<TreeView.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type model:BaustelleModel}" ItemsSource="{Binding Abschnitte}">
<Grid ShowGridLines="True">
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="A"/>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border Grid.Column="0" BorderThickness="0 0 1 0" BorderBrush="Black">
<Label Content="{Binding Bezeichnung}"/>
</Border>
<Label Grid.Column="1" Background="Red"/>
<Label Grid.Column="2" Background="Green"/>
<Label Grid.Column="3" Background="AliceBlue"/>
</Grid>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
The result is shown in the picture. However, I want to draw a border for evey node like the blue lines. If a new node is added, or a text changed, the border should align for all nodes with the border of the node, where the text end on the most far right.
Update 1
So, I fiddled around a bit with the ControlTemplate. And the problem is, that the ContentPresenter starts with an indention based on its level. In the picture below, the background of the ContentPresenter is set to red. So I need a way, to substract "level * space for indention" from the columnwidth.
I hope there is no direct way other than overriding the default TreeviewItem.
TreeViewItem Style:
<Style TargetType="TreeViewItem" BasedOn="{StaticResource {x:Type TreeViewItem}}">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TreeViewItem">
<StackPanel>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"
MinWidth="19" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<ToggleButton IsChecked="{Binding Path=IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press" Name="Expander">
<ToggleButton.Style>
<Style TargetType="ToggleButton">
<Setter Property="UIElement.Focusable" Value="false" />
<Setter Property="FrameworkElement.Width" Value="16" />
<Setter Property="FrameworkElement.Height" Value="16" />
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate TargetType="ToggleButton">
<Border Padding="5,5,5,5" Background="#00FFFFFF" Width="16" Height="16">
<Path Fill="#00FFFFFF" Stroke="#FF989898" Name="ExpandPath">
<Path.Data>
<PathGeometry Figures="M0,0L0,6L6,0z" />
</Path.Data>
<Path.RenderTransform>
<RotateTransform Angle="135" CenterX="3" CenterY="3" />
</Path.RenderTransform>
</Path>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="UIElement.IsMouseOver" Value="True">
<Setter TargetName="ExpandPath" Property="Shape.Stroke" Value="#FF1BBBFA" />
<Setter TargetName="ExpandPath" Property="Shape.Fill" Value="#00FFFFFF" />
</Trigger>
<Trigger Property="ToggleButton.IsChecked" Value="True">
<Setter TargetName="ExpandPath" Property="UIElement.RenderTransform">
<Setter.Value>
<RotateTransform Angle="180" CenterX="3" CenterY="3" />
</Setter.Value>
</Setter>
<Setter TargetName="ExpandPath" Property="Shape.Fill" Value="#FF595959" />
<Setter TargetName="ExpandPath" Property="Shape.Stroke" Value="#FF262626" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ToggleButton.Style>
</ToggleButton>
<Border x:Name="Bd"
HorizontalAlignment="Stretch"
BorderThickness="{TemplateBinding Border.BorderThickness}"
BorderBrush="{TemplateBinding Border.BorderBrush}"
Padding="{TemplateBinding Control.Padding}"
Background="{TemplateBinding Panel.Background}"
SnapsToDevicePixels="True"
Grid.Column="1">
<ContentPresenter x:Name="PART_Header"
Content="{TemplateBinding HeaderedContentControl.Header}"
ContentTemplate="{TemplateBinding HeaderedContentControl.HeaderTemplate}"
ContentStringFormat="{TemplateBinding HeaderedItemsControl.HeaderStringFormat}"
ContentTemplateSelector="{TemplateBinding HeaderedItemsControl.HeaderTemplateSelector}"
ContentSource="Header"
HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
</Border>
<ItemsPresenter x:Name="ItemsHost"
Grid.Column="1"
Grid.Row="1" />
</Grid>
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="TreeViewItem.IsExpanded" Value="False">
<Setter TargetName="ItemsHost" Property="UIElement.Visibility" Value="Collapsed" />
</Trigger>
<Trigger Property="ItemsControl.HasItems" Value="False">
<Setter TargetName="Expander" Property="UIElement.Visibility" Value="Hidden" />
</Trigger>
<Trigger Property="TreeViewItem.IsSelected" Value="True">
<Setter TargetName="Bd" Property="Panel.Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
<Setter Property="TextElement.Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="TreeViewItem.IsSelected" Value="True" />
<Condition Property="Selector.IsSelectionActive" Value="False" />
</MultiTrigger.Conditions>
<Setter TargetName="Bd" Property="Panel.Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
<Setter Property="TextElement.Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
</MultiTrigger>
<Trigger Property="UIElement.IsEnabled" Value="False">
<Setter Property="TextElement.Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Now you can have your code:
<TreeView x:Name="treeView">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:BaustelleModel}" ItemsSource="{Binding Abschnitte}">
<DockPanel LastChildFill="False">
<Border BorderThickness="0 0 1 0" BorderBrush="Black" DockPanel.Dock="Left">
<Label Content="{Binding Bezeichnung}"/>
</Border>
<StackPanel Orientation="Horizontal" DockPanel.Dock="Right">
<Label Background="Red" Width="50"/>
<Label Background="Green" Width="50"/>
<Label Background="AliceBlue" Width="50"/>
</StackPanel>
</DockPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
Output:
Reference: https://leecampbell.com/2009/01/14/horizontal-stretch-on-treeviewitems/
Hope that helps.

How to create a custom combox button?

Below is my combo box code,it has two combo box one inside other,now i want the combobox2 toggle button to change its direction of point,i.e the toggle button should point towards right and also on clicking that toggle button combo box should open on the right side so that it does not hide my combo box 1 items,how to do this?
<Window x:Class="ComboBox.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>
<Color x:Key="GrayColor_">#FF928B81</Color>
<Color x:Key="LightGrayColor_">#FFC3C3C3</Color>
<Color x:Key="LightLightGrayColor_">#FFF1F1F1</Color>
<SolidColorBrush x:Key="GrayColor" Color="{StaticResource GrayColor_}"/>
<SolidColorBrush x:Key="LightGrayColor" Color="{StaticResource LightGrayColor_}"/>
<SolidColorBrush x:Key="LightLightGrayColor" Color="{StaticResource LightLightGrayColor_}"/>
<Color x:Key="BlueColor_">#0073b0</Color>
<Color x:Key="DarkBlueColor_">#FF004165</Color>
<Color x:Key="LightBlueColor_">#FFa4ddfa</Color>
<SolidColorBrush x:Key="BlueColor" Color="{StaticResource BlueColor_}" />
<SolidColorBrush x:Key="DarkBlueColor" Color="{StaticResource DarkBlueColor_}" />
<SolidColorBrush x:Key="LightBlueColor" Color="{StaticResource LightBlueColor_}" />
<SolidColorBrush x:Key="Foreground" Color="Black"/>
<SolidColorBrush x:Key="ForegroundWhite" Color="White"/>
<Style x:Key="LightGrayBox" TargetType="{x:Type Border}">
<Setter Property="Background" Value="White"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="BorderBrush" Value="{StaticResource LightGrayColor}" />
</Style>
<Style x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToggleButton">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="20"/>
</Grid.ColumnDefinitions>
<Border x:Name="Border" Style="{DynamicResource LightGrayBox}" Grid.ColumnSpan="2" Background="{StaticResource BlueColor}" />
<Path x:Name="Arrow" Grid.Column="1" Opacity="0.6" Fill="{StaticResource LightBlueColor}" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 0 L 4 4 L 8 0 Z"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="ToggleButton.IsMouseOver" Value="true">
<Setter TargetName="Arrow" Property="Opacity" Value="1" />
<Setter TargetName="Arrow" Property="Fill" Value="{StaticResource LightBlueColor}" />
<Setter TargetName="Border" Property="BorderBrush" Value="{DynamicResource BlueColor}"/>
</Trigger>
<Trigger Property="ToggleButton.IsChecked" Value="true">
<Setter TargetName="Arrow" Property="Opacity" Value="1" />
<Setter TargetName="Arrow" Property="Fill" Value="{StaticResource LightBlueColor}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ComboBoxToggleButtonActive" TargetType="{x:Type ToggleButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToggleButton">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="20"/>
</Grid.ColumnDefinitions>
<Border x:Name="Border" Grid.ColumnSpan="2" Background="{DynamicResource BlueColor}" />
<Path x:Name="Arrow" Grid.Column="1" Opacity="1" Fill="{StaticResource LightBlueColor}" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 0 L 4 4 L 8 0 Z"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsFocused" Value="true">
<Setter TargetName="Border" Property="Background" Value="{DynamicResource BlueColor}" />
<Setter TargetName="Border" Property="BorderBrush" Value="White"/>
<Setter Property="Foreground" Value="White" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<ControlTemplate x:Key="ComboBoxTextBox" TargetType="TextBox">
<Border x:Name="PART_ContentHost" Background="{TemplateBinding Background}" />
</ControlTemplate>
<Style x:Key="StandardComboBox" TargetType="{x:Type ComboBox}">
<Setter Property="Foreground" Value="{StaticResource Foreground}"/>
<Setter Property="Height" Value="25"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="IsEditable" Value="True" />
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="Width" Value="120"/>
<Setter Property="IsSelected" Value="{Binding IsKeyboardFocusWithin, RelativeSource={RelativeSource Self}, Mode=OneWay}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBox">
<Grid>
<ToggleButton Name="ToggleButton" Style="{StaticResource ComboBoxToggleButton}" Grid.Column="2" Focusable="false" ClickMode="Press"
IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" />
<Label Name="ContentSite" IsHitTestVisible="False" Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Margin="3,3,23,3" VerticalAlignment="Center" HorizontalAlignment="Left" />
<!--<ContentPresenter Name="ContentSite" IsHitTestVisible="False" Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Margin="3,3,23,3" VerticalAlignment="Center" HorizontalAlignment="Left" />-->
<TextBox x:Name="PART_EditableTextBox"
CaretBrush="{DynamicResource ForegroundWhite}"
Style="{x:Null}"
Template="{StaticResource ComboBoxTextBox}"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Margin="3,3,23,3"
Focusable="True"
Background="Transparent"
Foreground="{StaticResource ForegroundWhite}"
Visibility="Hidden"
IsReadOnly="{TemplateBinding IsReadOnly}"/>
<Popup VerticalOffset="-1" SnapsToDevicePixels="True" Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsDropDownOpen}" AllowsTransparency="True" Focusable="False" PopupAnimation="Fade">
<Grid Name="DropDown" SnapsToDevicePixels="True" MinWidth="{TemplateBinding ActualWidth}" MaxHeight="200">
<Border x:Name="DropDownBorder" Style="{DynamicResource LightGrayBox}"/>
<ScrollViewer SnapsToDevicePixels="True">
<StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
</ScrollViewer>
</Grid>
</Popup>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="HasItems" Value="false">
<Setter TargetName="DropDownBorder" Property="MinHeight" Value="20"/>
</Trigger>
<Trigger Property="IsGrouping" Value="true">
<Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="ToggleButton" Property="Style" Value="{StaticResource ComboBoxToggleButtonActive}" />
<Setter TargetName="ContentSite" Property="Foreground" Value="{DynamicResource ForegroundWhite}"/>
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter TargetName="PART_EditableTextBox" Property="Foreground" Value="{DynamicResource Foreground}"/>
<Setter TargetName="ContentSite" Property="Foreground" Value="{DynamicResource Foreground}"/>
</Trigger>
<Trigger Property="IsEditable" Value="true">
<Setter Property="IsTabStop" Value="false"/>
<Setter TargetName="PART_EditableTextBox" Property="Visibility" Value="Visible"/>
<Setter TargetName="ContentSite" Property="Visibility" Value="Hidden"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Resources>
<Style TargetType="ComboBoxItem">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="HorizontalContentAlignment" Value="Left" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBoxItem">
<Border Name="Border" Padding="2" SnapsToDevicePixels="true" BorderThickness="1" Background="{StaticResource BlueColor}">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsHighlighted" Value="true">
<Setter TargetName="Border" Property="Background" Value="{DynamicResource BlueColor}"/>
<Setter TargetName="Border" Property="Padding" Value="2,3,2,3" />
<Setter Property="Foreground" Value="White" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Style.Resources>
</Style>
</Window.Resources>
<Grid>
<ComboBox Height="23" Margin="122,32,100,0" Style="{StaticResource StandardComboBox}" Name="comboBox1" VerticalAlignment="Top" IsEditable="True" Text="Settings">
<ComboBoxItem>
<ComboBox Style="{StaticResource StandardComboBox}" Name="comboBox2" VerticalAlignment="Top" >
<StackPanel Orientation="Horizontal">
<Label Content="Logout Time:"></Label>
<Label></Label>
</StackPanel>
<ComboBoxItem Content="10 Min"></ComboBoxItem>
<ComboBoxItem Content="20 Min"></ComboBoxItem>
<ComboBoxItem Content="30 Min"></ComboBoxItem>
<ComboBoxItem Content="40 Min"></ComboBoxItem>
<ComboBoxItem Content="50 Min"></ComboBoxItem>
</ComboBox>
</ComboBoxItem>
<ComboBoxItem Content="Logout"></ComboBoxItem>
</ComboBox>
</Grid>
</Window>
You will need to define a new ControlTemplate for your ComboBox control. You can find out how to define a new ControlTemplate in the Customizing the Appearance of an Existing Control by Using a ControlTemplate article on MSDN. A good place to start is by implementing the default ControlTemplate and then customising it as you see fit. You can find the default ControlTemplate of the ComboBox in the ComboBox Styles and Templates page on MSDN.
As a start point, find the following controls from the default ControlTemplate in the linked page on MSDN:
<ToggleButton x:Name="ToggleButton"
Template="{StaticResource ComboBoxToggleButton}"
Grid.Column="2"
Focusable="false"
ClickMode="Press"
IsChecked="{Binding IsDropDownOpen, Mode=TwoWay,
RelativeSource={RelativeSource TemplatedParent}}"/>
<ContentPresenter x:Name="ContentSite"
IsHitTestVisible="False"
Content="{TemplateBinding SelectionBoxItem}"
ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
Margin="3,3,23,3"
VerticalAlignment="Stretch"
HorizontalAlignment="Left">
</ContentPresenter>
<TextBox x:Name="PART_EditableTextBox"
Style="{x:Null}"
Template="{StaticResource ComboBoxTextBox}"
HorizontalAlignment="Left"
VerticalAlignment="Bottom"
Margin="3,3,23,3"
Focusable="True"
Background="Transparent"
Visibility="Hidden"
IsReadOnly="{TemplateBinding IsReadOnly}" />
The ToggleButton is clearly the Button within the ComboBox control, the ContentSite ContentPresenter is where the selected item is displayed next to the Button and the PART_EditableTextBox TextBox is the editable TextBox used when the control is set to IsEditable="True". If you can write XAML, then you can simply arrange these inner controls to suit your requirements.
UPDATE >>>
In order to rotate a control, the simplest way is to use a RotateTransform... try this:
<Path x:Name="Arrow" Grid.Column="1" HorizontalAlignment="Center"
VerticalAlignment="Center" Data="M 0 0 L 4 4 L 8 0 Z" >
<Path.Fill>
<SolidColorBrush Color="{DynamicResource GlyphColor}"/>
</Path.Fill>
<Path.LayoutTransform>
<RotateTransform Angle="90" />
</Path.LayoutTransform>
</Path>
UPDATE 2 (and hopefully last one) >>>
The ComboBox pop up is a Popup control, so to find out how to open it to the right, you need to research how to open a Popup to the right. For that, I'd take a look at the Positioning the Popup section of the Popup Placement Behavior page on MSDN.
Now, please bear in mind that we are not here to do all of your work for you. I feel I have armed you with more than enough information for you to complete your custom ComboBox control. Good luck.
Might help
<ComboBox Height="23" Margin="122,32,100,0" Style="{StaticResource StandardComboBox}" Name="comboBox1" VerticalAlignment="Top" IsEditable="True" Text="Settings">
<ComboBoxItem>
<ComboBox ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Hidden" Name="comboBox2" VerticalAlignment="Top">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal" IsItemsHost="True"/>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
<StackPanel Orientation="Horizontal">
<Label Content="Logout Time:"></Label>
<Label></Label>
</StackPanel>
<ComboBoxItem Content="10 Min"></ComboBoxItem>
<ComboBoxItem Content="20 Min"></ComboBoxItem>
<ComboBoxItem Content="30 Min"></ComboBoxItem>
<ComboBoxItem Content="40 Min"></ComboBoxItem>
<ComboBoxItem Content="50 Min"></ComboBoxItem>
</ComboBox>
</ComboBoxItem>
<ComboBoxItem Content="Logout">
</ComboBoxItem>
</ComboBox>
</Grid>

Custom WPF ComboBox doesn't show grouping

I have created a custom ComboBox in WPF
<!-- Combobox template -->
<ControlTemplate x:Key="CustomComboBoxToggleButton" TargetType="ToggleButton">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="20"/>
</Grid.ColumnDefinitions>
<Border x:Name="Border" Grid.ColumnSpan="2" CornerRadius="3" Background="{StaticResource CustomDarkBlue}" BorderBrush="{StaticResource CustomWhite}" BorderThickness="2"/>
<Border x:Name="Border2" Grid.Column="0" CornerRadius="3,0,0,3" Margin="1" Background="{StaticResource CustomBackground}" BorderBrush="{StaticResource CustomWhite}" BorderThickness="1,1,2,1"/>
<Path x:Name="Arrow" Grid.Column="1" Fill="{StaticResource CustomGlyph}" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 0 L 4 4 L 8 0 Z"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="ToggleButton.IsMouseOver" Value="True">
<Setter TargetName="Border" Property="Background" Value="{StaticResource CustomDarkBlue}"/>
</Trigger>
<Trigger Property="ToggleButton.IsChecked" Value="True">
<Setter TargetName="Border" Property="Background" Value="{StaticResource CustomDarkBlue}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource CustomGray}"/>
<Setter TargetName="Border2" Property="BorderBrush" Value="{StaticResource CustomGray}"/>
<Setter Property="Foreground" Value="{StaticResource CustomGray}"/>
<Setter TargetName="Arrow" Property="Fill" Value="{StaticResource CustomGray}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<!-- Combobox -->
<Style x:Key="RoundedComboBox" TargetType="{x:Type ComboBox}">
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="True"/>
<Setter Property="Foreground" Value="{StaticResource CustomWhite}"/>
<Setter Property="Background" Value="{StaticResource CustomBackground}"/>
<Setter Property="Height" Value="21"/>
<Setter Property="FontSize" Value="14"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBox">
<Grid>
<ToggleButton Name="ToggleButton" Template="{StaticResource CustomComboBoxToggleButton}" Grid.Column="2" Focusable="False" IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press"/>
<ContentPresenter Name="ContentSite" IsHitTestVisible="False" Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}" Margin="3,1,23,1" VerticalAlignment="Center" HorizontalAlignment="Left"/>
<Popup Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsDropDownOpen}" AllowsTransparency="True" Focusable="False" PopupAnimation="Slide">
<Grid Name="DropDown" SnapsToDevicePixels="True" MinWidth="{TemplateBinding ActualWidth}" MaxHeight="{TemplateBinding MaxDropDownHeight}">
<Border x:Name="DropDownBorder" Background="{StaticResource CustomBackground}" BorderThickness="2" BorderBrush="{StaticResource CustomWhite}"/>
<ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True">
<StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained"/>
</ScrollViewer>
</Grid>
</Popup>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="HasItems" Value="False">
<Setter TargetName="DropDownBorder" Property="MinHeight" Value="95"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="{StaticResource CustomGray}"/>
</Trigger>
<Trigger Property="IsGrouping" Value="True">
<Setter Property="ScrollViewer.CanContentScroll" Value="False"/>
</Trigger>
<Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="True">
<Setter TargetName="DropDownBorder" Property="CornerRadius" Value="4"/>
<Setter TargetName="DropDownBorder" Property="Margin" Value="0,2,0,0"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
This is then applied to a ComboBox
<ComboBox Grid.Column="0" x:Name="cboChild" Margin="140,65,0,0" ItemsSource="{Binding Source={StaticResource GroupedData}}" ItemTemplate="{StaticResource AccountTemplate}" SelectedValue="{Binding Source={StaticResource Item}, Path=child}" SelectedValuePath="ID" Width="150" Style="{StaticResource RoundedComboBox}" HorizontalAlignment="Left" VerticalAlignment="Top">
<ComboBox.GroupStyle>
<GroupStyle HeaderTemplate="{StaticResource GroupHeader}"/>
</ComboBox.GroupStyle>
</ComboBox>
The following is found in Window.Resources
<Window.Resources>
<CollectionViewSource Source="{Binding ChildAccounts}" x:Key="GroupedData">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="group"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
<DataTemplate x:Key="GroupHeader">
<TextBlock Text="{Binding group}" Margin="0" Foreground="{StaticResource CustomWhite}"/>
</DataTemplate>
<DataTemplate x:Key="AccountTemplate">
<TextBlock Text="{Binding comment}" Margin="0" Foreground="{StaticResource CustomWhite}"/>
</DataTemplate>
</Window.Resources>
Basically grouping works as it should when the RoundedComboBox style is NOT applied (ie. to the default ComboBox) but whenever the style is applied the drop-down popup is empty
Help!!
Thanks
Andy
Found the answer..... Use <ItemsPresenter ...> instead of <StackPanel ...>
Thanks sa_ddam213 for your helpful advice above regarding the binding.

Resources