【问题标题】:In wpf how can I set the value of a dependency property in a custom control在 wpf 中,如何在自定义控件中设置依赖属性的值
【发布时间】:2012-05-16 09:15:47
【问题描述】:

我创建了一个 WPF UserCotrol。在其中,我有 3 个默认为
visibility="collapsed" 的网格。我创建了一个这样的依赖属性:

public int PanelId
    {
        get { return (int)GetValue(PanelIdProperty); }
        set { SetValue(PanelIdProperty, value); }
    }

public static readonly DependencyProperty PanelIdProperty =
        DependencyProperty.Register("PanelId", typeof(int), typeof(RecurrencePattern), new UIPropertyMetadata(1));

我想在另一个 xaml 中使用这个用户控件。我这样声明:

<uc:RecurrencePattern PanelId="2"/>

我认为通过这样做,PanelId 将是 2,并且在运行时的默认构造函数中,我可以使用它来设置可见的面板。 相反,PanelId 是 UIPropertyMetadata(1) 定义的 1。如何使用 xaml 中提供的值来设置哪个网格可见。 我有:

<Grid x:Name="a" Visibility="Collapsed">
    <label Content"a"/>  
</Grid>
<Grid x:Name="b" Visibility="Collapsed">
    <label Content"b"/>  
</Grid>
<Grid x:Name="c" Visibility="Collapsed">
    <label Content"c"/>  
</Grid>

默认构造函数是这样的:

switch (PanelId)
  {
    case 1:
      a.Visibility = System.Windows.Visibility.Visible;
      break;
    case 2:
      b.Visibility = System.Windows.Visibility.Visible;
      break;
    case 3:
      c.Visibility = System.Windows.Visibility.Visible;
      break;
    default:
      a.Visibility = System.Windows.Visibility.Visible;
      break;
}

谢谢。

【问题讨论】:

  • 在构造函数完成之前永远不会执行属性设置器。你的方法不会像这样工作。将代码从构造函数移动到例如OnInitialized.
  • AngelWPF 是对的。但是对于您要在这里实现的目标,您甚至不需要依赖属性。您可以使用简单的 CLR 属性并在 OnInitialized 中检查其值。

标签: wpf user-controls dependency-properties


【解决方案1】:

Visibility更改的代码需要在依赖属性更改事件中......

  public static readonly DependencyProperty PanelIdProperty
     = DependencyProperty.Register(
          "PanelId",
          typeof(int),
          typeof(RecurrencePattern),
          new UIPropertyMetadata(1, PanelIdPropertyChangedCallback)); 

    private static void PanelIdPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var recurrencePattern = d as RecurrencePattern;
        if (recurrencePattern != null)
        {
            var panelId = Convert.ToInt32(e.NewValue);
            switch (panelId)
            {
                case 1:
                    recurrencePattern.Visibility
                       = System.Windows.Visibility.Visible;
                    break;
                case 2:
                    recurrencePattern.Visibility
                       = System.Windows.Visibility.Visible;
                    break;
                case 3:
                    recurrencePattern.Visibility 
                       = System.Windows.Visibility.Visible;
                    break;
                default:
                    recurrencePattern.Visibility 
                       = System.Windows.Visibility.Visible;
                    break;
            }
        }
    }

希望这会有所帮助...

【讨论】:

  • 这不是自己设置用户控件的可见性吗?不是 RecurrencePattern UserControl 内部的网格 a 或 b 或 c。
  • 好的,我做到了。我只需要recurrencePattern.a.Visibility = System.Windows.Visibility.Visible;等等...非常感谢...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多