My name is Edward Tanguay. I'm an American software and web developer living and working in Berlin, Germany.


4 hours ago: If you are a developer in Berlin and need to improve your English, I'm looking for groups to teach after work: http://tanguay.info/itenglish.
5 hours ago: As far as I'm concerned, the singularity is already here, every time I wake up twitter tells me something amazing was created while I slept.
5 hours ago: We're not suffering from information overload, we're suffering from faulty filtering.
6 hours ago: Classic literature for free as nicely formatted 1-page or 2-page PDF downloads: http://www.planetebook.com/free-ebooks.asp.
6 hours ago: Yes, when you pour coffee, "a lightning storm of neuronal activity occurs almost across the entire brain": http://is.gd/eWO1T @pholdings.
22 hours ago: If you put two spaces after a period or use underlining for emphasis, you were born before 1980.
23 hours ago: Word of the day: infovore, n. an animal with a voracious appetite for information.
yesterday: It's said that on average people use less than 10% of their brain, but I think on average computers use less than 1% of their CPU.
2 days ago: Saturday fun: team drawing on two computers with six-year-old in a shared google doc diagram.
2 days ago: Someday I want to produce a developer podcast called "What's that?" but for now "the developer's life" is a nice genre: http://is.gd/eTURO.
3 days ago: Here's a use-case for datapod format, recording human-readable data that later can be used as a datasource: http://is.gd/eSsLg @pholdings.
Custom ChooserGroup control which allows user to change selection kind from dropdown to radiobuttons This is an example of how to make a control that abstracts the dropdown and radiobutton controls away so that you can make a "ChooserGroup" and then render it as a: (1) dropdown, (2) radio buttons vertical, or (3) radio buttons horizontal. This control has a custom OnChoiceItemChanged event which even abstracts the handling of a changed item no matter what type of control has been rendered. ![]() > > > Download Code XAML:
<Window x:Class="TestRad2343.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" WindowStartupLocation="CenterScreen" Title="Window1" Height="350" Width="300"> <Window.Resources> <Style TargetType="{x:Type GroupBox}" x:Key="GroupBoxMain"> <Setter Property="Padding" Value="10" /> <Setter Property="Margin" Value="0 0 0 10" /> <Setter Property="BorderBrush" Value="#ccc" /> <Setter Property="BorderThickness" Value="2" /> </Style> <Style TargetType="{x:Type TextBlock}" x:Key="GroupBoxHeaderMain"> <Setter Property="FontFamily" Value="Lucida Sans" /> <Setter Property="FontSize" Value="12" /> <Setter Property="Foreground" Value="#aaa" /> </Style> </Window.Resources> <StackPanel Margin="10" HorizontalAlignment="Left"> <GroupBox Style="{DynamicResource GroupBoxMain}" Margin="0 0 0 10"> <GroupBox.Header> <TextBlock Text="Switch the Chooser" Style="{DynamicResource GroupBoxHeaderMain}"/> </GroupBox.Header> <StackPanel> <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" > <Button x:Name="ButtonForDropDown" Content="Drop Down" Padding="3" Margin="0 0 0 3" Click="Button_Click_DropDown"/> <Button x:Name="ButtonForRadioButtonsHorizontal" Content="Radio Buttons Horizontal" Padding="3" Margin="0 0 0 3" Click="Button_Click_RadioButtonsHorizontal" /> <Button x:Name="ButtonForRadioButtonsVertical" Content="Radio Buttons Vertical" Padding="3" Margin="0 0 0 3" Click="Button_Click_RadioButtonsVertical"/> </StackPanel> </StackPanel> </GroupBox> <StackPanel HorizontalAlignment="Left" Margin="0 0 0 10"> <ContentControl x:Name="ControlArea"/> </StackPanel> <StackPanel HorizontalAlignment="Left" Margin="0 0 0 10"> <Button x:Name="SubmitButton" Content="Submit" Click="Button_Click_Submit"/> </StackPanel> <TextBlock x:Name="Message" Foreground="Blue" Margin="0 0 0 10"/> </StackPanel> </Window> Code Behind:
using System; using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Globalization; using System.ComponentModel; namespace TestRad2343 { public partial class Window1 : Window, INotifyPropertyChanged { #region ViewModelProperty: SelectedIdCode private string _selectedIdCode; public string SelectedIdCode { get { return _selectedIdCode; } set { _selectedIdCode = value; OnPropertyChanged("SelectedIdCode"); } } #endregion //test private ChooserDirection chooserDirection = new ChooserDirection(); private ChooserKind chooserKind = new ChooserKind(); private string choices = "one, two, three, four, five, six"; public Window1() { InitializeComponent(); chooserKind = ChooserKind.RadioButtons; chooserDirection = ChooserDirection.Vertical; ManageButtonEnabling("radioButtonsVertical"); SelectedIdCode = "two"; RefreshPage(); } void RefreshPage() { WrapPanel wp = new WrapPanel(); ChooserGroup chooserGroup = new ChooserGroup("chooser1", choices, SelectedIdCode); chooserGroup.ChooserKind = chooserKind; chooserGroup.ChooserDirection = chooserDirection; ControlArea.Content = chooserGroup.Render(); chooserGroup.OnChoiceItemChanged += new ChooserGroup.ChoiceItemChangedHandler(chooserGroup_OnChoiceItemChanged); SubmitButton.Tag = chooserGroup; } void chooserGroup_OnChoiceItemChanged(object sender, ChoiceItemArgs args) { ChooserGroup chooserGroup = sender as ChooserGroup; SelectedIdCode = chooserGroup.GetSelectedIdCode(); } private void Button_Click_Submit(object sender, RoutedEventArgs e) { ChooserGroup chooserGroup = ((Button)sender).Tag as ChooserGroup; Message.Text = String.Format("You selected '{0}'.", chooserGroup.GetSelectedIdCode()); } private void Button_Click_DropDown(object sender, RoutedEventArgs e) { chooserKind = ChooserKind.DropDown; ManageButtonEnabling("dropDown"); RefreshPage(); } private void Button_Click_RadioButtonsHorizontal(object sender, RoutedEventArgs e) { chooserKind = ChooserKind.RadioButtons; chooserDirection = ChooserDirection.Horizontal; ManageButtonEnabling("radioButtonsHoriztonal"); RefreshPage(); } private void Button_Click_RadioButtonsVertical(object sender, RoutedEventArgs e) { chooserKind = ChooserKind.RadioButtons; chooserDirection = ChooserDirection.Vertical; ManageButtonEnabling("radioButtonsVertical"); RefreshPage(); } public void ManageButtonEnabling(string buttonToEnable) { switch (buttonToEnable) { case "dropDown": ButtonForDropDown.IsEnabled = false; ButtonForRadioButtonsHorizontal.IsEnabled = true; ButtonForRadioButtonsVertical.IsEnabled = true; break; case "radioButtonsHoriztonal": ButtonForDropDown.IsEnabled = true; ButtonForRadioButtonsHorizontal.IsEnabled = false; ButtonForRadioButtonsVertical.IsEnabled = true; break; case "radioButtonsVertical": ButtonForDropDown.IsEnabled = true; ButtonForRadioButtonsHorizontal.IsEnabled = true; ButtonForRadioButtonsVertical.IsEnabled = false; break; default: break; } } #region INotifiedProperty Block public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); } } #endregion //test } public class ChooserGroup { public ChooserDirection ChooserDirection { get; set; } public ChooserKind ChooserKind { get; set; } private string idCode; private List<ChoiceItem> choiceItems = new List<ChoiceItem>(); private List<RadioButton> internalRadioButtons = new List<RadioButton>(); private ComboBox internalComboBox = new ComboBox(); private List<ComboBoxItem> internalComboBoxItems = new List<ComboBoxItem>(); public delegate void ChoiceItemChangedHandler(object obj, ChoiceItemArgs args); public event ChoiceItemChangedHandler OnChoiceItemChanged; public ChooserGroup(string chooserIdCode, string choiceList, string selectedChoice) { idCode = chooserIdCode; List<string> choices = choiceList.BreakIntoParts(','); choiceItems = ChoiceItem.GetChoiceItemsWithStringList(choices, selectedChoice); ChooserKind = ChooserKind.RadioButtons; ChooserDirection = ChooserDirection.Vertical; } public StackPanel Render() { switch (ChooserKind) { case ChooserKind.RadioButtons: return RenderAsRadioButtons(); case ChooserKind.DropDown: return RenderAsDropDown(); default: return new StackPanel(); } } StackPanel RenderAsDropDown() { StackPanel spWrapper = new StackPanel(); int index = 0; foreach (var choiceItem in choiceItems) { ComboBoxItem comboBoxItem = new ComboBoxItem { Tag = choiceItem.IdCode, Content = choiceItem.Title }; internalComboBox.Items.Add(comboBoxItem); if (choiceItem.IsSelected) internalComboBox.SelectedIndex = index; index++; internalComboBoxItems.Add(comboBoxItem); } if (internalComboBox.SelectedItem == null) { if (internalComboBox.Items.Count > 0) internalComboBox.SelectedItem = internalComboBox.Items[0]; } spWrapper.Children.Add(internalComboBox); internalComboBox.SelectionChanged += new SelectionChangedEventHandler(internalComboBox_SelectionChanged); return spWrapper; } void internalComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { ChoiceItemArgs args = new ChoiceItemArgs(""); OnChoiceItemChanged(this, args); } StackPanel RenderAsRadioButtons() { StackPanel spWrapper = new StackPanel(); StackPanel spDirectionWrapper = new StackPanel(); WrapPanel wpDirectionWrapper = new WrapPanel(); foreach (var choiceItem in choiceItems) { RadioButton rb = new RadioButton(); rb.Content = choiceItem.Title; rb.Tag = choiceItem.IdCode; rb.GroupName = idCode; rb.Cursor = Cursors.Hand; if (choiceItem.IsSelected) rb.IsChecked = true; else rb.IsChecked = false; if (ChooserDirection == ChooserDirection.Horizontal) { rb.Margin = new Thickness(5); wpDirectionWrapper.Children.Add(rb); } else { spDirectionWrapper.Children.Add(rb); } rb.Checked += new RoutedEventHandler(rb_Checked); internalRadioButtons.Add(rb); } if (ChooserDirection == ChooserDirection.Horizontal) spWrapper.Children.Add(wpDirectionWrapper); else spWrapper.Children.Add(spDirectionWrapper); return spWrapper; } void rb_Checked(object sender, RoutedEventArgs e) { ChoiceItemArgs args = new ChoiceItemArgs(""); OnChoiceItemChanged(this, args); } public string GetSelectedIdCode() { switch (ChooserKind) { case ChooserKind.RadioButtons: return GetSelectedIdCodeFromRadioButtons(); case ChooserKind.DropDown: return GetSelectedIdCodeFromDropDown(); default: return ""; } } string GetSelectedIdCodeFromRadioButtons() { foreach (var internalRadioButton in internalRadioButtons) { if (internalRadioButton.IsChecked.Value) return internalRadioButton.Tag.ToString(); } return null; } string GetSelectedIdCodeFromDropDown() { int selectedIndex = internalComboBox.SelectedIndex; ComboBoxItem selectedComboBoxItem = internalComboBoxItems[selectedIndex]; return selectedComboBoxItem.Tag.ToString(); } } public class ChoiceItemArgs : EventArgs { public string Message { get; set; } public ChoiceItemArgs(string message) { Message = message; } } public enum ChooserDirection { Horizontal, Vertical } public enum ChooserKind { DropDown, RadioButtons } public class ChoiceItem { public string IdCode { get; set; } public string Title { get; set; } public bool IsSelected { get; set; } public static List<ChoiceItem> GetChoiceItemsWithStringList(List<string> rawChoices, string selectedValue) { if (rawChoices == null) return null; List<ChoiceItem> choiceItems = new List<ChoiceItem>(); foreach (var rawChoice in rawChoices) { ChoiceItem ci = new ChoiceItem(); ci.IdCode = rawChoice; TextInfo text = new CultureInfo("en-US", false).TextInfo; ci.Title = text.ToTitleCase(rawChoice); if (ci.IdCode == selectedValue) ci.IsSelected = true; choiceItems.Add(ci); } return choiceItems; } } public static class StringHelpers { public static List<string> BreakIntoParts(this string line, char separator) { if (String.IsNullOrEmpty(line)) return null; else return line.Split(separator).Select(p => p.Trim()).ToList(); } } } |
Most Recently Added Items:
- Extension method for checking regex in one line - c# code example - added 4 days ago
- How to use a Dictionary<> with struct key to save a dynamic matrix of objects - c# code example - added on Sunday, August 22, 2010
- A simple jquery search machine for a web page - jquery code example - added on Sunday, August 22, 2010
- Wrapper class to simplify the creation of Excel files in C# 4.0 - wpf code example - added on Tuesday, July 20, 2010
- How to make clickable flashcards in plain javascript for your mobile phone - javascript code example - added on Wednesday, July 07, 2010
- Simple example of javascript which loads jquery locally - jquery code example - added on Wednesday, July 07, 2010
- How to stop regular expression greediness - c# code example - added on Tuesday, July 06, 2010
- How to use a generic dictionary to total enum values - c# code example - added on Friday, July 02, 2010
- Generic method to case-insensitively convert a string to any enum - c# code example - added on Friday, July 02, 2010
- How to create a TextBlock that has various font formatting in code behind - silverlight code example - added on Wednesday, June 30, 2010
- How to encode binary files to text files and back to binary again - c# code example - added on Wednesday, June 30, 2010
- How to use a custom parameter struct to pass any number of variables to constructors of similar classes. - c# code example - added on Saturday, June 26, 2010
- How to make a class that renders an interactive FrameworkElement and interacts with the View - silverlight code example - added on Tuesday, June 15, 2010
- How to set a nullable type to null in a ternary operator - c# code example - added on Tuesday, June 15, 2010
- How to override events in inherited classes - c# code example - added on Friday, June 11, 2010
- How to strip off e.g. "note:" and "firstName: " from the left of a string using regex - c# code example - added on Tuesday, June 01, 2010
- How to create and subscribe to custom events using EventHandler - c# code example - added on Wednesday, May 12, 2010
- Code base for asynchronously loading and caching dependent data in a Silverlight app - silverlight code example - added on Wednesday, May 05, 2010
- Function to trim the preceding and trailing blank lines off an array - php code example - added on Sunday, May 02, 2010
- How to load and display the contents of a text file with AJAX/Jquery - jquery code example - added on Sunday, May 02, 2010
- How to use fopen() to create a proxy site to read any website content into AJAX - php code example - added on Sunday, May 02, 2010
- Code base for loading and caching external data into a silverlight app - silverlight code example - added on Friday, April 30, 2010
- An UpdateSourceTrigger workaround for Silverlight - wpf code example - added on Sunday, April 18, 2010
- Silverlight layout with click-in info panel - silverlight code example - added on Thursday, April 01, 2010
- How to get querystring variables and change HTML elements from Silverlight. - silverlight code example - added on Thursday, April 01, 2010
- XAML and code for a basic chat window in WPF - wpf code example - added on Saturday, March 27, 2010
- How to consume text from any Google Document, RSS feed, or Twitter feed in your Silverlight application - wpf code example - added on Sunday, March 21, 2010
- How to overlay one image on top of another in code behind - wpf code example - added on Saturday, March 20, 2010
- How to get the mouse-click and mouseover coordinates from an image - wpf code example - added on Tuesday, March 16, 2010
- How to use OrderedDictionary to lookup items by string key or integer index - c# code example - added on Tuesday, March 16, 2010
