Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SelectButton for WPF (similar to a SplitButton)
using System;
using System.Collections;
using System.Windows;
using System.Windows.Controls;
namespace loraderon.Controls
{
public partial class SelectButton : UserControl
{
public event RoutedEventHandler Click;
public event SelectionChangedEventHandler SelectionChanged;
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(SelectButton));
public static readonly DependencyProperty ItemTemplateProperty =
DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(SelectButton), new UIPropertyMetadata(null));
public static readonly DependencyProperty IsExpandedProperty =
DependencyProperty.Register("IsExpanded", typeof(bool), typeof(SelectButton));
public static readonly DependencyProperty SelectedIndexProperty =
DependencyProperty.Register("SelectedIndex", typeof(int), typeof(SelectButton));
public SelectButton()
{
InitializeComponent();
}
public IEnumerable ItemsSource
{
get { return (IEnumerable)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
public DataTemplate ItemTemplate
{
get { return (DataTemplate)GetValue(ItemTemplateProperty); }
set { SetValue(ItemTemplateProperty, value); }
}
public int SelectedIndex
{
get { return (int)GetValue(SelectedIndexProperty); }
set { SetValue(SelectedIndexProperty, value); }
}
public bool IsExpanded
{
get { return (bool)GetValue(IsExpandedProperty); }
set
{
SetValue(IsExpandedProperty, value);
if (!value)
collapsedAt = DateTime.Now;
if (value)
ListBox.Focus();
}
}
private DateTime collapsedAt = DateTime.MinValue;
private void Expander_Expanded(object sender, RoutedEventArgs e)
{
if (DateTime.Now - collapsedAt <= TimeSpan.FromMilliseconds(200))
{
Expander.IsExpanded = false;
IsExpanded = false;
return;
}
IsExpanded = true;
}
private void Expander_Collapsed(object sender, RoutedEventArgs e)
{
IsExpanded = false;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (Click != null)
Click(this, e);
}
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ButtonContent.Content = ListBox.SelectedItem;
if (SelectionChanged != null)
SelectionChanged(this, e);
IsExpanded = false;
}
private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
{
ButtonContent.Width = SplitGrid.ColumnDefinitions[0].ActualWidth;
}
}
}
<UserControl x:Class="loraderon.Controls.SelectButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:my="clr-namespace:loraderon.Controls"
mc:Ignorable="d"
SizeChanged="UserControl_SizeChanged"
d:DesignHeight="30" d:DesignWidth="100">
<Grid
x:Name="SplitGrid"
>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="23" />
</Grid.ColumnDefinitions>
<Button
x:Name="Button"
Click="Button_Click"
Grid.ColumnSpan="2"
Padding="0"
HorizontalContentAlignment="Left"
>
<ContentControl
x:Name="ButtonContent"
HorizontalContentAlignment="Center"
ContentTemplate="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type my:SelectButton}}, Path=ItemTemplate}"
/>
</Button>
<Expander
x:Name="Expander"
Expanded="Expander_Expanded"
Collapsed="Expander_Collapsed"
Grid.Column="1"
VerticalAlignment="Center"
IsExpanded="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type my:SelectButton}}, Path=IsExpanded}"
/>
<Popup
IsOpen="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type my:SelectButton}}, Path=IsExpanded}"
PlacementTarget="{Binding ElementName=Button}"
PopupAnimation="Fade"
StaysOpen="False"
>
<ListBox
x:Name="ListBox"
SelectionMode="Single"
SelectionChanged="ListBox_SelectionChanged"
SelectedIndex="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type my:SelectButton}}, Path=SelectedIndex, Mode=TwoWay}"
ItemTemplate="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type my:SelectButton}}, Path=ItemTemplate}"
ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type my:SelectButton}}, Path=ItemsSource}"
/>
</Popup>
</Grid>
</UserControl>
@loraderon

This comment has been minimized.

Copy link
Owner Author

loraderon commented Sep 15, 2010

SelectButton preview

SelectButton preview

@siasur

This comment has been minimized.

Copy link

siasur commented Nov 7, 2013

Hi,

did you have any Preview Code. I'm new at WPF and i don't know how can i use the code to get a result seen on your picture.

I know that this is old... but i hope you read (and understand :D ) this ^^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.