Skip to content

Instantly share code, notes, and snippets.

@naveedmurtuza
Created July 11, 2013 12:05
Show Gist options
  • Save naveedmurtuza/5974874 to your computer and use it in GitHub Desktop.
Save naveedmurtuza/5974874 to your computer and use it in GitHub Desktop.
CheckedComboBox Custom Control WPF
using System;
using System.Collections;
using System.Text;
using System.Windows;
using System.Windows.Controls;
namespace CryptoTools.UI.Controls
{
public class CheckedComboBox : ComboBox
{
private ListBox _listbox;
static CheckedComboBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CheckedComboBox), new FrameworkPropertyMetadata(typeof(CheckedComboBox)));
}
public static readonly DependencyProperty DefaultTextProperty =
DependencyProperty.Register("DefaultText", typeof (String), typeof (CheckedComboBox), new PropertyMetadata(default(String)));
public String DefaultText
{
get { return (String) GetValue(DefaultTextProperty); }
set { SetValue(DefaultTextProperty, value); }
}
public static readonly DependencyProperty SelectedItemsTextProperty =
DependencyProperty.Register("SelectedItemsText", typeof(String), typeof(CheckedComboBox), new FrameworkPropertyMetadata()
{
BindsTwoWayByDefault = true
});
public String SelectedItemsText
{
get { return (String)GetValue(SelectedItemsTextProperty); }
set { SetValue(SelectedItemsTextProperty, value); }
}
public static readonly DependencyProperty DelimeterProperty =
DependencyProperty.Register("Delimeter", typeof (String), typeof (CheckedComboBox), new PropertyMetadata(","));
public String Delimeter
{
get { return (String) GetValue(DelimeterProperty); }
set { SetValue(DelimeterProperty, value); }
}
public IList SelectedItems
{
get { return _listbox.SelectedItems; }
}
protected override void OnDropDownClosed(EventArgs e)
{
base.OnDropDownClosed(e);
ToolTip = GetFormattedText(Environment.NewLine);
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
if (this.Template != null)
{
_listbox = (ListBox)Template.FindName("ListBox", this);
_listbox.SelectionChanged += (sender, args) =>
{
OnSelectionChanged(args);
UpdateText();
//Console.WriteLine("========");
//foreach (var selectedItem in _listbox.SelectedItems)
//{
// Console.WriteLine(selectedItem);
//}
//Console.WriteLine("========");
};
}
}
private void UpdateText()
{
if(_listbox == null || _listbox.SelectedItems.Count == 0)
{
SelectedItemsText = DefaultText;
return;
}
Console.WriteLine(GetFormattedText(Delimeter));
SelectedItemsText = GetFormattedText(Delimeter);
}
private String GetFormattedText(String delimeter)
{
var sb = new StringBuilder();
foreach (var selectedItem in _listbox.SelectedItems)
{
sb.Append(selectedItem).Append(delimeter);
}
//chop off the last delimeter
sb.Remove(sb.Length - delimeter.Length, delimeter.Length);
return sb.ToString();
}
}
}
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:CryptoTools="clr-namespace:CryptoTools"
xmlns:Controls="clr-namespace:CryptoTools.UI.Controls">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../Resources/Colors.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type Controls:CheckedComboBox}">
<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="MinWidth" Value="120" />
<Setter Property="MinHeight" Value="20" />
<Setter Property="Foreground" Value="{StaticResource ForegroundBrush}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Controls:CheckedComboBox">
<Grid x:Name="MainGrid">
<ToggleButton Name="ToggleButton" Template="{StaticResource ComboBoxToggleButton}"
Grid.Column="2" Focusable="false"
IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press">
</ToggleButton>
<ContentPresenter Name="ContentSite" IsHitTestVisible="False"
Content="{TemplateBinding SelectedItemsText}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Margin="3,3,23,3" VerticalAlignment="Center"
HorizontalAlignment="Left" />
<TextBox x:Name="PART_EditableTextBox" CaretBrush="White" Foreground="White" Style="{x:Null}"
Template="{StaticResource ComboBoxTextBox}" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="3,3,23,3"
Focusable="True" Background="Transparent" Visibility="Hidden" IsReadOnly="{TemplateBinding IsReadOnly}" />
<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 BackgroundBrush}"
BorderThickness="1" BorderBrush="{StaticResource BorderBrush}" />
<ScrollViewer Name="ScrollView" Margin="4,6,4,6" SnapsToDevicePixels="True">
<ListBox Name="ListBox" ItemsSource="{Binding ItemsSource,RelativeSource={RelativeSource AncestorType=Controls:CheckedComboBox}}" SelectionMode="Multiple">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Content,RelativeSource={RelativeSource AncestorType=ListBoxItem}}" IsChecked="{Binding IsSelected,RelativeSource={RelativeSource AncestorType=ListBoxItem}}" Name="PART_Checkbox" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</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 DisabledBrush}" />
</Trigger>
<Trigger Property="IsGrouping" Value="true">
<Setter Property="ScrollViewer.CanContentScroll" Value="false" />
</Trigger>
<Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="true">
</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.Triggers></Style.Triggers>
</Style>
</ResourceDictionary>
@hazem010
Copy link

ComboBoxToggleButton not exit
& it gives me error
'System.Windows.Style' is not a valid value for property 'Template'.

i don't know how to use it in right way
thanks in advance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment