开发者

Set property on usercontrol that can be used in custom panel in control... Silverlight

I have a simple usercontrol that uses a simple custom panel where I just overrode the Orientation and Measure functions.

What I want to do is to have a property in the usercontol to control the orientation

So I basicaly have

UserControl
 --> Listbox
   --> MyPanel

And I want a property for the usercontrol that can be set in xaml (of type System.Windows.Controls.Orientation ) that I can bind to from my custom panel (or a different approach if binding isnt the right way to do it)

It would be a bonus if that property could show up in the properties window and you could select vertical or horizontal.

And a super bonus if I could change the property at desig开发者_StackOverflow社区n time and have the listbox/


First of all you would add a Orientation property to your 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));

The way you bind to it from MyPanel is via the UserControl's root element. Give the root element a name (typically this is a Grid with the name "LayoutRoot").

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

I dunno about the properties window but that should just work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜