0

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); }
        }
    }
}
Patrick Pirzer
  • 1,649
  • 3
  • 22
  • 49
  • Try to use `null` as default value. If you inherit from `RadioButton` (you don't show cs), then you have to [override default value](http://stackoverflow.com/a/5653472/1997232) and not creating a new property. – Sinatr May 09 '16 at 14:30
  • I tried null as default value for the DependencyProperty GroupNameProperty, but the result is the same. After i clicked on all radiobuttons they are all enabled. – Patrick Pirzer May 09 '16 at 14:37
  • Okay, i added the code of the CustomControl to my posting. Maybe it's now better to understand. – Patrick Pirzer May 09 '16 at 14:40
  • Meanwhile i even tried to override the default value of the IsCheckedProperty in the constructor of my CustomControl and removed my new DependencyProperties for IsCheckedProperty and GroupNameProperty. I can set the properties in the WPF-application where i use the control, but the result is still the same without setting GroupName. – Patrick Pirzer May 09 '16 at 14:55
  • 1
    `RadioButton` already have [`GroupName`](https://msdn.microsoft.com/en-us/library/system.windows.controls.radiobutton.groupname(v=vs.110).aspx) , your control will inherit it, can you tell me why are you creating new? Don't and it should work. Moreover, maybe you don't need custom control, in wpf this is rarely needed (with all power of re-styling and attached properties). – Sinatr May 09 '16 at 15:21
  • We are making these CustomControls because we are working on a dynamic WPF-designer in which the users can create their own WPF-windows. So we need to add some additional properties to the standard controls. Normally the CustomControls are working fine, except the RadioButton. – Patrick Pirzer May 10 '16 at 06:37
  • Meanwhile i corrected my code - according to Your proposals - as You can see above. – Patrick Pirzer May 10 '16 at 06:52

1 Answers1

0

Meanwhile i decided to override the GroupNameProperty with a default value. This works as usual and the users have the possibility to change the GroupName, so it even works with multiple groups of RadioButtons.

    /// <summary>
    /// The constructor.
    /// </summary>
    static CustomRadioButton()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomRadioButton),
       new FrameworkPropertyMetadata(typeof(CustomRadioButton)));

        [...]

        // Override the default value of the base-DependencyProperty GroupName, so the RadioButtons are synchronized as usual.
        // For usage of multiple groups the property GroupName has be set explicitly - as usual.
        GroupNameProperty.OverrideMetadata(typeof(CustomRadioButton),
       new FrameworkPropertyMetadata("group0"));
    }

Supplement: Instead of a groupname like "group0" above i can use a blank (" "). This let the RadioButtons work as usual. But an empty string as groupname doesn't work.

Patrick Pirzer
  • 1,649
  • 3
  • 22
  • 49