【发布时间】: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 中这很少需要(具有重新样式和附加属性的所有功能)。