Hi,
There are no immediate plans to add radio button components to MindFusion.Diagramming but you can implement your own with relative ease. Simply derive from CheckBoxComponent, define custom images for the checked and unchecked states of your component through the ImageList and CheckedImageList properties, then override the OnClicked event and ensure that only one radio button can be selected at a time. For the later, you can use the notion of radio button groups: when one of the buttons in the group gets checked, the other components are automatically unchecked. Here is the code that implements this scenario:
public class RadioButtonComponent : CheckBoxComponent
{
public RadioButtonComponent()
{
this.ImageList = new List<Image>();
this.ImageList.AddRange(new Image[]
{
Resources.Unchecked_Normal,
Resources.Unchecked_Down,
Resources.Unchecked_Disabled,
Resources.Unchecked_Over
});
this.CheckedImageList = new List<Image>();
this.CheckedImageList.AddRange(new Image[]
{
Resources.Checked_Normal,
Resources.Checked_Down,
Resources.Checked_Disabled,
Resources.Checked_Over
});
}
protected override void OnClicked(EventArgs e)
{
base.OnClicked(e);
IsChecked = true;
if (group != null)
group.Toggle(this);
}
public RadioButtonGroup Group
{
get { return group; }
set
{
if (group != null)
group.Remove(this);
group = value;
if (group != null)
group.Add(this);
}
}
private RadioButtonGroup group;
}
public class RadioButtonGroup
{
public RadioButtonGroup()
{
buttons = new List<RadioButtonComponent>();
}
internal void Add(RadioButtonComponent radioButton)
{
buttons.Add(radioButton);
}
internal void Remove(RadioButtonComponent radioButton)
{
buttons.Remove(radioButton);
}
internal void Toggle(RadioButtonComponent radioButton)
{
foreach (RadioButtonComponent button in buttons)
{
if (button == radioButton)
continue;
button.IsChecked = false;
}
}
private List<RadioButtonComponent> buttons;
}
Below is a code snippet, which creates a CompositeNode with several radio buttons from an XML string. Note that the new RadioButtonComponent type will be automatically resolved by the XmlLoader as long as the RadioButtonComponent class is public.
CompositeNode n = new CompositeNode();
n.Components.Add(XmlLoader.Load(@"
<Border Background='Silver' Padding='2'>
<StackPanel Orientation='Vertical' Spacing='1'>
<StackPanel Orientation='Horizontal' Spacing='1'>
<RadioButtonComponent Name='rb1' HorizontalAlignment='Left' VerticalAlignment='Center' Focusable='False' />
<Text Text='Option #1' Font='Arial, 8pt' />
</StackPanel>
<StackPanel Orientation='Horizontal' Spacing='1'>
<RadioButtonComponent Name='rb2' HorizontalAlignment='Left' VerticalAlignment='Center' Focusable='False' />
<Text Text='Option #2' Font='Arial, 8pt' />
</StackPanel>
<StackPanel Orientation='Horizontal' Spacing='1'>
<RadioButtonComponent Name='rb3' HorizontalAlignment='Left' VerticalAlignment='Center' Focusable='False' />
<Text Text='Option #3' Font='Arial, 8pt' />
</StackPanel>
</StackPanel>
</Border>"));
RadioButtonGroup group = new RadioButtonGroup();
(n.FindComponent("rb1") as RadioButtonComponent).Group = group;
(n.FindComponent("rb2") as RadioButtonComponent).Group = group;
(n.FindComponent("rb3") as RadioButtonComponent).Group = group;
n.Bounds = new RectangleF(10, 10, 50, 50);
diagram1.Nodes.Add(n);
And here is an image illustrating the end result:
Regards,
Meppy