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


2 days ago: Oh great, another "it depends" answer, isn't anything simple anymore? http://t.co/ypAZDhvX.
2 days ago: 24K sunday training: three cold 5Ks 24:14, 26:48, 29:13, 61 days to #parismarathon: http://t.co/pRWmrVui.
2 days ago: 5th grade humor: "your arithmetic is terrible, where did you learn math? / yale / yale? what's your name? / yim yonson".
4 days ago: News in slow french: nice way to keep your french up to par: audio, transcription, hover-vocabulary: http://t.co/ykqztLlM.
4 days ago: Newsinslowenglish: nice way to improve English: audio, transcription, hover-vocabulary #tesl #esl #efl: http://t.co/ehq08wQD.
4 days ago: Overheard at home: "I like my sister 23%".
4 days ago: This is the way I used to teach German pronunciation in college, fun memories: http://t.co/eRULGfun.
4 days ago: "excuse me, but, um, your location is leaking".
4 days ago: "our vision for photography in berlin is for it to be lively, progressive and active" #berlin #photography @just4you http://t.co/0I59GQeV.
4 days ago: Facebook will eventually find a way to stamp out privacy: "everyone famous to their friends & therefore public figures" http://t.co/p4V9RLSA.
4 days ago: .@nickhoward there's a global layer of educational incompetence surfacing everywhere where people want to teach instead of help people learn.
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:
- How to call an unknown function on an unknown class with unknown parameters - php code example - added on Sunday, November 28, 2010
- How to setup JQuery intellisense in Microsoft Visual Web Developer 2010 Express - jquery howto - added on Sunday, November 14, 2010
- Simple menu site with jQuery AJAX - jquery code example - added on Sunday, November 14, 2010
- A PHP function which returns the most frequently occuring item - php code example - added on Friday, October 29, 2010
- How to create a singleton in PHP - php code example - added on Sunday, October 03, 2010
- Extension method to sort a generic collection of objects - c# code example - added on Tuesday, September 07, 2010
- A simple class that represents a matching quiz item - c# code example - added on Tuesday, September 07, 2010
- Extension method for checking regex in one line - c# code example - added on Thursday, September 02, 2010
- 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
