【问题标题】:Set property on usercontrol that can be used in custom panel in control... Silverlight在用户控件上设置可在控件中的自定义面板中使用的属性... Silverlight
【发布时间】:2010-06-08 16:30:27
【问题描述】:

我有一个简单的用户控件,它使用一个简单的自定义面板,我只是覆盖了 Orientation 和 Measure 函数。

我想做的是在用户控件中有一个属性来控制方向

所以我基本上有

UserControl
 --> Listbox
   --> MyPanel

我想要一个可以在 xaml 中设置的用户控件属性(类型为 System.Windows.Controls.Orientation ),我可以从我的自定义面板绑定到该属性(如果绑定不是正确的方法,则可以使用不同的方法)它)

如果该属性可以显示在属性窗口中并且您可以选择垂直或水平,那将是一个奖励。

如果我可以在设计时更改属性并拥有列表框/

【问题讨论】:

    标签: silverlight user-controls listbox panel


    【解决方案1】:

    首先,您需要将Orientation 属性添加到您的UserControl:-

        public Orientation Orientation
        {
            get { return (Orientation)GetValue(OrientationProperty); }
            set { SetValue(OrientationProperty, value); }
        }
    
        public static readonly DependencyProperty OrientationProperty =
                DependencyProperty.Register(
                        "Orientation",
                        typeof(Orientation),
                        typeof(YourNewUserControl),
                        new PropertyMetadata(Orientation.Vertical));
    

    您从 MyPanel 绑定到它的方式是通过 UserControl 的根元素。为根元素命名(通常是名称为“LayoutRoot”的Grid)。

    <ListBox ...>
      <ListBox.ItemsPanel>
         <ItemsPanelTemplate>
            <MyPanel Orientation="{Binding Parent.Orientation, ElementName=LayoutRoot}" />
         <ItemsPanelTemplate>
      </ListBox.ItemsPanel>
    

    我不知道属性窗口,但这应该可以工作。

    【讨论】:

    • 我不确定我是否错过了什么,但没有运气,但谢谢。首先,当尝试绑定到 Parent.anything 时,它会爆炸。你的语句不是绑定到网格的方向属性(x:Name="LayoutRoot")而不是用户控件的依赖属性吗?
    • 没有。问:“LayoutRoot”的父级是什么? - 答:用户控件。因此,假设您已将 Orientation 属性添加到 UserControl,则来自 LayoutRoot 的属性路径 Parent.Orientation 应该可以工作。在我的机器上做 ;)
    • 我的错,我必须解决它或发布代码。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-24
    • 2012-02-18
    相关资源
    最近更新 更多