从RadioButton继承的WPF-CustomControl分组只适用于GroupName

时间:2016-05-09 14:05:12

标签: c# wpf xaml

我在WPF-CustomControlLibrary中创建了一个CustomControl,它继承自RadioButton。

在CustomControl CustomRadioButton的构造函数中,我用另一个默认值覆盖DependyProperty IsCheckedProperty(在我的例子中为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));
}

只要我在应用程序中设置,我使用CustomControlLibrary,RadioButtons的属性GroupName,分组机制就可以工作。

如果在Click之后启用了一个RadioButton,则禁用所有其他(属于同一组)。

但是如果我没有设置属性GroupName,则分组不再起作用。

如果我点击一个RadioButton,它将永远启用。

如果没有明确设置属性GroupName,如何保证分组?

提前致谢!

这里是我在Generic.xaml中的CustomControl的定义:

<!--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>

来自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); }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

同时我决定使用默认值覆盖GroupNameProperty。 这照常工作,用户可以更改GroupName,因此它甚至适用于多组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"));
    }

补充: 我可以使用空格(“”)代替上面的“group0”这样的组名。这让RadioButtons像往常一样工作。 但是作为groupname的空字符串不起作用。