tags:

views:

26

answers:

1

Hi have ItemsControl in my main Grid and PanelTemplate as wrapPanel. In this wrapPanel I am putting MyCustomControl. I want to set width of the MycustomControl dynamically. Also I need acess to this width in MyCustomControl's onLoaded event. Can anybody help?

A: 

I am assuming you are looking to put your custom control (MyCustomControl) into a visual container (in this case a WrapPanel).

I have found that looking for ActualWidth and ActualHeight properties in the OnLoaded event doesn't work as the layout process in Silverlight has not finished.

Every visual container implements a two stage process responsible for sizing and arranging its child controls.

To give an example, here is a panel class implementing the two stage process just mentioned (MeasureOverride() and ArrangeOverride()).

public class RowPanel : Panel
    {
        private const double _groupSpace = 80;

        public Point Location;

        public RowPanel ()
            : base()
        {
            Location = new Point();
        }

        protected override Size MeasureOverride ( Size availableSize )
        {
            Size size = new Size( double.PositiveInfinity, double.PositiveInfinity );
            foreach ( UIElement child in Children )
                child.Measure( size );

            return this.ArrangeNodes( false );
        }

        protected override Size ArrangeOverride ( Size finalSize )
        {
            return this.ArrangeNodes( true );
        }

        private Size ArrangeNodes ( bool arrange )
        {
            double pos = 0d;
            Rect bounds = new Rect();
            Size totalSize = new Size( 0, 0 );

            foreach ( UIElement child in Children )
            {
                GroupPanel group = child as GroupPanel;

                bounds.X = pos;
                bounds.Y = 0;

                bounds.Width = group.DesiredSize.Width;
                bounds.Height = group.DesiredSize.Height;

                if ( arrange )
                {
                    group.Arrange( bounds );
                    Point b = new Point( bounds.X, bounds.Y );
                    group.SetValue( GroupPanel.LocationProperty, b );
                }

                totalSize.Width = pos + group.DesiredSize.Width;
                totalSize.Height = Math.Max( totalSize.Height, group.DesiredSize.Height );

                pos += ( bounds.Width + RowPanel._groupSpace );
            }

            return totalSize;
        }
    }

What I'd suggest is that you look to implement the MeasureOverride and ArrangeOverride for you visual container and you custom control.

Rus
<ItemsControl x:Name="itemsControl" Grid.Row="0"> <ItemsControl.ItemTemplate> <DataTemplate><Page:MyCustomControl x:Name="itemPanel" Tag="{Binding}"></Page:MyCustomControl></DataTemplate></ItemsControl.ItemTemplate></ItemsControl> This is main page
jolly
Actually I want to set desired width of myCustomControl. I have tried measureOverride and ArrangeOverride method but still not working. MyCustom control contains ContentControl to which I am applying different styles in loaded event. Below is the xaml for My customControl <Border><Border > <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" x:Name="LayoutRoot" Background="#DAE0E5" Tag="{Binding}"> <ContentControl x:Name="mainContent" Width="{Binding Width, ElementName=LayoutRoot}" ></ContentControl> </StackPanel> </Border> </Border>
jolly