如何定义具有两个子依赖属性的扩展stackpanel控件

本文关键字:属性 依赖 扩展 控件 stackpanel 两个 何定义 定义 | 更新日期: 2023-09-27 18:11:10

我必须写一个ExtendedStackPanel控件,它将有两个这样的依赖属性。

<ExtendedStackPanel  IsReadOnly="{Binding Item.IsReadOnly, Mode=OneWay}"  >
     <TemplateTrue>
                         ... control visible when isreadonly is true  
      </TemplateTrue>
      <TemplateFalse>
                           ... control visible when isreadonly is false
       </TemplateFalse>
</ExtendedStackPanel>

我写这篇文章是为了达到目的,但它不起作用。

public class ExtendedStackPanel : StackPanel
{
    public ExtendedStackPanel()
        : base()
    {
        this.Orientation = System.Windows.Controls.Orientation.Vertical;
    }
    #region IsReadOnly
    public bool IsReadOnly
    {
        get { return (bool)GetValue(IsReadOnlyProperty); }
        set { SetValue(IsReadOnlyProperty, value); }
    }
    public static readonly DependencyProperty IsReadOnlyProperty =
        DependencyProperty.Register("IsReadOnly", typeof(bool),
            typeof(ExtendedStackPanel), new PropertyMetadata(new PropertyChangedCallback(OnReadOnlyChanged)));
    static void OnReadOnlyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var visible = (bool)e.NewValue;
        var control = d as ExtendedStackPanel;
        if (visible)
        {
            control.TemplateTrue.Visibility = Visibility.Visible;
            control.TemplateFalse.Visibility = Visibility.Collapsed;
        }
        else
        {
            control.TemplateTrue.Visibility = Visibility.Collapsed;
            control.TemplateFalse.Visibility = Visibility.Visible;
        }
    }
    #endregion
    #region TemplateTrue
    public Control TemplateTrue
    {
        get { return (Control)GetValue(TemplateTrueProperty); }
        set { SetValue(TemplateTrueProperty, value); }
    }
    public static readonly DependencyProperty TemplateTrueProperty =
            DependencyProperty.Register("TemplateTrue", typeof(Control),
            typeof(ExtendedStackPanel), new PropertyMetadata(new PropertyChangedCallback(OnTemplateTrueChanged)));
    static void OnTemplateTrueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var control = d as ExtendedStackPanel;
        control.TemplateTrue.Visibility = control.IsReadOnly ? Visibility.Visible : Visibility.Collapsed;
    }
    #endregion
    #region TemplateFalse
    public Control TemplateFalse
    {
        get { return (Control)GetValue(TemplateFalseProperty); }
        set { SetValue(TemplateFalseProperty, value); }
    }
    public static readonly DependencyProperty TemplateFalseProperty =
            DependencyProperty.Register("TemplateFalse", typeof(Control),
            typeof(ExtendedStackPanel), new PropertyMetadata(new PropertyChangedCallback(OnTemplateFalseChanged)));
    static void OnTemplateFalseChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var control = d as ExtendedStackPanel;
        control.TemplateFalse.Visibility = !control.IsReadOnly ? Visibility.Visible : Visibility.Collapsed;
    }
    #endregion
}

在我的代码中,当IsReadOnly设置为false时,我放置了一个组合框控件,当IsReadOnly设置为true时,一个简单的文本框,但当代码运行时什么都不显示。

如何定义具有两个子依赖属性的扩展stackpanel控件

最后,我选择使用UserControl,在其中放入两个ContentControl。在UserControl后面的代码中,我这样写:

public partial class ConditionalControl : UserControl, INotifyPropertyChanged
{
    public ConditionalControl()
    {
        InitializeComponent();
        this.IsReadOnly = false;
    }
    #region IsReadOnly
    public bool IsReadOnly
    {
        get { return (bool)GetValue(IsReadOnlyProperty); }
        set { SetValue(IsReadOnlyProperty, value); }
    }
    public static readonly DependencyProperty IsReadOnlyProperty =
        DependencyProperty.Register("IsReadOnly", typeof(bool),
            typeof(ConditionalControl), new PropertyMetadata(new PropertyChangedCallback(OnReadOnlyChanged)));
    static void OnReadOnlyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var visible = (bool)e.NewValue;
        var control = d as ConditionalControl;            
        if (visible)
        {
            control.TemplateTrueContent.Visibility = Visibility.Visible;
            control.TemplateFalseContent.Visibility = Visibility.Collapsed;
        }
        else
        {
            control.TemplateTrueContent.Visibility = Visibility.Collapsed;
            control.TemplateFalseContent.Visibility = Visibility.Visible;
        }
    }
    #endregion
    #region TemplateFalse
    public object TemplateFalse
    {
        get { return (object)GetValue(TemplateFalseProperty); }
        set { SetValue(TemplateFalseProperty, value); }
    }
    public static readonly DependencyProperty TemplateFalseProperty =
            DependencyProperty.Register("TemplateFalse", typeof(object),
            typeof(ConditionalControl), new PropertyMetadata(new PropertyChangedCallback(OnTemplateFalseChanged)));
    static void OnTemplateFalseChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var control = d as ConditionalControl;
        control.TemplateFalseContent.Visibility = !control.IsReadOnly ? Visibility.Visible : Visibility.Collapsed;
        control.OnPropertyChanged("TemplateFalse");
    }
    #endregion
    #region TemplateTrue
    public object TemplateTrue
    {
        get { return (object)GetValue(TemplateTrueProperty); }
        set { SetValue(TemplateTrueProperty, value); }
    }
    public static readonly DependencyProperty TemplateTrueProperty =
            DependencyProperty.Register("TemplateTrue", typeof(object),
            typeof(ConditionalControl), new PropertyMetadata(new PropertyChangedCallback(OnTemplateTrueChanged)));
    static void OnTemplateTrueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var control = d as ConditionalControl;
        control.TemplateTrueContent.Visibility = control.IsReadOnly ? Visibility.Visible : Visibility.Collapsed;
        control.OnPropertyChanged("TemplateTrue");
    }
    #endregion
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName)
    {
        if (propertyName == "TemplateFalse")
        {
            this.TemplateFalseContent.Content = this.TemplateFalse;
        }
        if (propertyName == "TemplateTrue")
        {
            this.TemplateTrueContent.Content = this.TemplateTrue;
        }
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

希望对大家有所帮助