【问题标题】:Grouping of WPF-CustomControl, which inherits from RadioButton, just works with GroupName继承自 RadioButton 的 WPF-CustomControl 的分组仅适用于 GroupName
【发布时间】:2016-05-09 14:05:11
【问题描述】:

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

在 CustomControl CustomRadioButton 的构造函数中,我用另一个默认值(在我的情况下为 false)覆盖 DependyProperty IsCheckedProperty。

/// <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,分组机制就可以工作。

如果在单击后启用了一个 RadioButton,则所有其他 RadioButton(属于同一组)都将被禁用。

但是如果我不设置属性 GroupName,分组就不再起作用了。

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

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

提前致谢!

这里是我的 CustomControl 在 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>

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

【问题讨论】:

  • 尝试使用null作为默认值。如果你继承自RadioButton(你不显示cs),那么你必须override default value而不是创建一个新属性。
  • 我尝试将 null 作为 DependencyProperty GroupNameProperty 的默认值,但结果是一样的。在我单击所有单选按钮后,它们都已启用。
  • 好的,我将 CustomControl 的代码添加到我的帖子中。也许现在更好理解。
  • 同时,我什至尝试在 CustomControl 的构造函数中覆盖 IsCheckedProperty 的默认值,并删除了 IsCheckedProperty 和 GroupNameProperty 的新 DependencyProperties。我可以在使用控件的 WPF 应用程序中设置属性,但结果仍然相同,无需设置 GroupName。
  • RadioButton 已经有 GroupName ,你的控件会继承它,你能告诉我你为什么要创建新的吗?不要,它应该工作。此外,也许您不需要自定义控件,在 wpf 中这很少需要(具有重新样式和附加属性的所有功能)。

标签: c# wpf xaml


【解决方案1】:

同时我决定用默认值覆盖 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 像往常一样工作。 但是作为组名的空字符串不起作用。

【讨论】:

  • 有趣的事实:默认组名可以是空白但不是 string.Empty。
猜你喜欢
  • 2017-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-24
相关资源
最近更新 更多