我做了一个UserControl,里面放了三个Button,然后我需要给这个控件设置三个属性,其中一个是:
CloseButton
属性是bool类型,如果设置为false,则控件中对应的button就不显示。
我通过DependencyProperty.Register很容易就做好了这个功能,但是让我困惑的是,在实际使用这个控件的时候,我发现:

系统会自动列出CloseButton属性,并且还有实际的那个按钮“CloseBtn”,我想既然CloseBtn能够直接访问,那么我还有啥必要另外做一个属性呢?直接:
xxx.CloseBtn.Visibility=Visibility.Hidden
去设置不是更好吗?
我试过如果在控件中不给按钮设置“name”属性,虽然不会列出该属性了,但是这也导致我在“OnCloseButtonChanged”代码中无法找到这个按钮:
public partial class SysButtonPanel : UserControl
{
public SysButtonPanel()
{
InitializeComponent();
}
public static readonly DependencyProperty CloseButtonProperty = DependencyProperty.Register("CloseButton", typeof(bool), typeof(SysButtonPanel), new PropertyMetadata(true, new PropertyChangedCallback(OnCloseButtonChanged)));
public bool CloseButton
{
get { return (bool)GetValue(CloseButtonProperty); }
set { SetValue(CloseButtonProperty, value); }
}
static void OnCloseButtonChanged(object sender, DependencyPropertyChangedEventArgs args)
{
SysButtonPanel source = (SysButtonPanel)sender;
//source.CloseBtn.Visibility = (bool)args.NewValue ? Visibility.Visible : Visibility.Hidden;
}
}
da jia hao!