views:

24

answers:

1

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 design time and have the listbox/

A: 

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.

AnthonyWJones
I am not sure if I missed something, but no luck, but thanks.First when trying to bind to Parent.anything, it bombs.Doesn't your statement bind to the orientation property of the grid( x:Name="LayoutRoot") and not the dependency property of the usercontrol?
Dimestore Cowboy
no. Q: What is the Parent of the "LayoutRoot"? A: The UserControl. Hence assuming you have added the `Orientation` property to the `UserControl` the property path `Parent.Orientation` from the LayoutRoot should work. Does on my machine ;)
AnthonyWJones
My bad, I'll have to work it out or post the code. Thank's for the help!
Dimestore Cowboy