I have made in a WPF-CustomControlLibrary a CustomControl, which inherits from the RadioButton.
In the constructor of the CustomControl CustomRadioButton i override the DependyProperty IsCheckedProperty with another default value (in my case false).
/// <summary>
/// The constructor.
/// </summary>
static CustomRadioButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomRadioButton), new FrameworkPropertyMetadata(typeof(CustomRadioButton)));
// Override the default value of the base-DependencyProperty.
IsCheckedProperty.OverrideMetadata(typeof(CustomRadioButton), new FrameworkPropertyMetadata(false));
}
As long as i set in the application, where i use the CustomControlLibrary, the property GroupName of the RadioButtons, the grouping mechanism works.
If one RadioButton is enabled after Click, all others (which belong to the same group) are disabled.
But if i don't set the property GroupName, the grouping doesn't work anymore.
If i click on a RadioButton, it will be enabled forever.
How can the grouping be garanteed without explicit setting of the property GroupName?
Thanks in advance!
And here the definition of my CustomControl in Generic.xaml:
<!--Style for the CustomControl CustomRadioButton-->
<Style TargetType="{x:Type local:CustomRadioButton}" BasedOn="{StaticResource {x:Type RadioButton}}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomRadioButton}">
<Border BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<RadioButton IsChecked="{TemplateBinding IsChecked}"
GroupName="{TemplateBinding GroupName}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}">
<TextBlock Text="{TemplateBinding Text}"
TextWrapping="Wrap"
TextAlignment="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type RadioButton}},
Path=HorizontalContentAlignment, Converter={StaticResource h2tAlignmentConverter}}"
TextDecorations="{TemplateBinding TextDecorations}"/>
</RadioButton>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
And the code from the CustomControl:
using System.Windows;
using System.Windows.Controls;
namespace WpfDesignerCustomControlLibrary
{
/// <summary>
/// Class for a CustomControl which inherits from RadioButton and has a nested TextBlock for textwrapping.
/// </summary>
public class CustomRadioButton : RadioButton
{
/// <summary>
/// The constructor.
/// </summary>
static CustomRadioButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomRadioButton), new FrameworkPropertyMetadata(typeof(CustomRadioButton)));
// Override the default value of the base-DependencyProperty.
IsCheckedProperty.OverrideMetadata(typeof(CustomRadioButton), new FrameworkPropertyMetadata(false));
}
/// <summary>
/// DependencyProperty for access to the text value.
/// </summary>
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(CustomRadioButton));
/// <summary>
/// DependencyProperty for the textdecorations.
/// </summary>
public static readonly DependencyProperty TextDecorationsProperty =
DependencyProperty.Register("TextDecorations", typeof(TextDecorationCollection), typeof(CustomRadioButton));
/// <summary>
/// DependencyProperty for external access to the property TableName.
/// </summary>
public static readonly DependencyProperty TableNameProperty =
DependencyProperty.Register("TableName", typeof(string), typeof(CustomRadioButton));
/// <summary>
/// Gets or sets the content.
/// </summary>
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
/// <summary>
/// Gets or sets the textdecorations.
/// </summary>
public TextDecorationCollection TextDecorations
{
get { return (TextDecorationCollection)GetValue(TextDecorationsProperty); }
set { SetValue(TextDecorationsProperty, value); }
}
/// <summary>
/// Gets or sets the name of the datatable which is the datasource.
/// </summary>
public string TableName
{
get { return (string)GetValue(TableNameProperty); }
set { SetValue(TableNameProperty, value); }
}
}
}