Click here to Skip to main content
15,887,027 members
Home / Discussions / WPF
   

WPF

 
AnswerRe: Confused about MarkupExtension class reference in XAML Pin
Pete O'Hanlon9-Jul-14 5:55
mvePete O'Hanlon9-Jul-14 5:55 
GeneralRe: Confused about MarkupExtension class reference in XAML Pin
Richard Deeming9-Jul-14 8:01
mveRichard Deeming9-Jul-14 8:01 
GeneralRe: Confused about MarkupExtension class reference in XAML Pin
Pete O'Hanlon9-Jul-14 8:33
mvePete O'Hanlon9-Jul-14 8:33 
QuestionNeed some idea for resizing my control Pin
Super Lloyd8-Jul-14 4:22
Super Lloyd8-Jul-14 4:22 
QuestionShow items in multiple columns Pin
pjank423-Jul-14 22:06
pjank423-Jul-14 22:06 
AnswerRe: Show items in multiple columns Pin
Pete O'Hanlon3-Jul-14 23:03
mvePete O'Hanlon3-Jul-14 23:03 
GeneralRe: Show items in multiple columns Pin
pjank424-Jul-14 5:26
pjank424-Jul-14 5:26 
QuestionMultipass MeasureOverride() not calculating updated items size (added code sample) Pin
Super Lloyd2-Jul-14 3:50
Super Lloyd2-Jul-14 3:50 
I have a custom Panel with custom controls which should resize like RibbonButton, i.e. as the available size vary I should remove / add a header, resize an inner picture, etc...

Further the custom controls are lookless control, i.e. UI/Template is defined in the style, and I use triggers to update template element.

I communicate size setting from the panel to the individual controls by using an inherited attached property (I mean UI inheritance, like DataContext)

When I do my multi pass MeasureOverride() in my panel, when I call child.Measure(), it's as if my items are always the same size!!
Despite me changing the inherited attached property (and it does update inside control!)
To be clear my inside control don't resize, but when I change the inherited property, they change their UI template (to a smaller version)

Any clue how I can successfully implement RibbonButon and RibbonGroupPanel like behavior?

EDIT I just fixed my sample! Laugh | :laugh:

To give you an idea here is the code for the shorter sample I could write:

Size123Panel & Size123Control
C#
	public class Size123Control : Control
{
	static Size123Control()
	{
		DefaultStyleKeyProperty.OverrideMetadata(typeof(Size123Control), new FrameworkPropertyMetadata(typeof(Size123Control)));
	}
}

public class Size123Panel : Panel
{
    public static int GetSize123(DependencyObject obj) { return (int)obj.GetValue(Size123Property); }

    public static void SetSize123(DependencyObject obj, int value) { obj.SetValue(Size123Property, value); }

	public static readonly DependencyProperty Size123Property = DependencyProperty.RegisterAttached(
		"Size123", typeof(int), typeof(Size123Panel), 
		new FrameworkPropertyMetadata(3,
			FrameworkPropertyMetadataOptions.Inherits
			| FrameworkPropertyMetadataOptions.AffectsMeasure
			| FrameworkPropertyMetadataOptions.AffectsParentMeasure
			| FrameworkPropertyMetadataOptions.AffectsArrange
			| FrameworkPropertyMetadataOptions.AffectsParentArrange
			));

    protected override Size ArrangeOverride(Size finalSize)
    {
        double hTot = 0, h = 0, x = 0, xMax = 0;
        foreach (UIElement child in Children)
        {
            var cs = child.DesiredSize;
            if (x > 0 && x + cs.Width > finalSize.Width)
            {
                hTot += h;
                x = 0;
                h = cs.Height;
            }
            else
            {
                if (cs.Height > h)
                    h = cs.Height;
            }
            child.Arrange(new Rect(new Point(x, hTot), cs));
            x += cs.Width;
            if (x > xMax)
                xMax = x;
        }
        return new Size(xMax, hTot + h);
    }

    protected override Size MeasureOverride(Size availableSize)
    {
        Func<Size> measure = () =>
        {
            double hTot = 0, h = 0, x = 0, xMax = 0;
            foreach (UIElement child in Children)
            {
                child.Measure(availableSize);
                var cs = child.DesiredSize;

                if (x > 0 && x + cs.Width > availableSize.Width)
                {
                    hTot += h;
                    x = 0;
                    h = cs.Height;
                }
                else
                {
                    if (cs.Height > h)
                        h = cs.Height;
                }
                x += cs.Width;
                if (x > xMax)
                    xMax = x;
            }
            return new Size(xMax, hTot + h);
        };

        SetSize123(this, 3);
        var s1 = measure();
        if (s1.Width <= availableSize.Width && s1.Height <= availableSize.Height)
            return s1;

        SetSize123(this, 2);
        var s2 = measure();
        if (s2.Width <= availableSize.Width && s2.Height <= availableSize.Height)
            return s2;

        SetSize123(this, 1);
        var s3 = measure();

        Console.WriteLine("Size123: {0}, {1}, {2}", s1, s2, s3);
        return s3;
    }
}


generic.xaml
XML
<Style TargetType="{x:Type local:Size123Control}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:Size123Control}">
                <Border x:Name="MAIN" Width="48" Height="48" Background="Red" BorderThickness="1" BorderBrush="LightGray"/>
                <ControlTemplate.Triggers>
                    <Trigger Property="local:Size123Panel.Size123" Value="2">
                        <Setter TargetName="MAIN" Property="Background" Value="Green"/>
                        <Setter TargetName="MAIN" Property="Width" Value="32"/>
                        <Setter TargetName="MAIN" Property="Height" Value="32"/>
                    </Trigger>
                    <Trigger Property="local:Size123Panel.Size123" Value="1">
                        <Setter TargetName="MAIN" Property="Background" Value="Blue"/>
                        <Setter TargetName="MAIN" Property="Width" Value="16"/>
                        <Setter TargetName="MAIN" Property="Height" Value="16"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>


And the test window:
XML
<local:Size123Panel>
    <local:Size123Control/>
    <local:Size123Control/>
    <local:Size123Control/>
    <local:Size123Control/>
    <local:Size123Control/>
    <local:Size123Control/>
    <local:Size123Control/>
    <local:Size123Control/>
    <local:Size123Control/>
    <local:Size123Control/>
    <local:Size123Control/>
    <local:Size123Control/>
    <local:Size123Control/>
</local:Size123Panel>


If you look the console, when there is not enough space and control are shrunk, all 3 computed size are the same!
My programming get away... The Blog...
DirectX for WinRT/C# since 2013!
Taking over the world since 1371!


modified 3-Jul-14 5:48am.

QuestionDraw a rectangular box Pin
Subin Mavunkal1-Jul-14 23:24
Subin Mavunkal1-Jul-14 23:24 
AnswerRe: Draw a rectangular box Pin
Matt T Heffron2-Jul-14 13:53
professionalMatt T Heffron2-Jul-14 13:53 
QuestionXAML Pin
Member 97193591-Jul-14 22:21
Member 97193591-Jul-14 22:21 
AnswerRe: XAML Pin
Pete O'Hanlon1-Jul-14 22:55
mvePete O'Hanlon1-Jul-14 22:55 
QuestionWPF Expander Problem Pin
Kevin Marois1-Jul-14 13:49
professionalKevin Marois1-Jul-14 13:49 
AnswerRe: WPF Expander Problem Pin
Pete O'Hanlon1-Jul-14 19:11
mvePete O'Hanlon1-Jul-14 19:11 
QuestionWPF - PRism Framework Pin
Muhammed Nigil29-Jun-14 23:36
professionalMuhammed Nigil29-Jun-14 23:36 
AnswerRe: WPF - PRism Framework Pin
Pete O'Hanlon30-Jun-14 0:17
mvePete O'Hanlon30-Jun-14 0:17 
GeneralRe: WPF - PRism Framework Pin
Muhammed Nigil30-Jun-14 1:00
professionalMuhammed Nigil30-Jun-14 1:00 
GeneralRe: WPF - PRism Framework Pin
Pete O'Hanlon30-Jun-14 3:03
mvePete O'Hanlon30-Jun-14 3:03 
GeneralRe: WPF - PRism Framework Pin
Muhammed Nigil30-Jun-14 18:56
professionalMuhammed Nigil30-Jun-14 18:56 
GeneralRe: WPF - PRism Framework Pin
Muhammed Nigil30-Jun-14 19:26
professionalMuhammed Nigil30-Jun-14 19:26 
QuestionHow to display from nested usercontrol another usercontrol class on button click event? Pin
LAPEC29-Jun-14 7:12
LAPEC29-Jun-14 7:12 
QuestionHow to Display .aspx page in SilverlightWeb Application Pin
RavitejaPammi28-Jun-14 5:22
RavitejaPammi28-Jun-14 5:22 
Questionsilverlight Pin
Member 1087962427-Jun-14 19:40
Member 1087962427-Jun-14 19:40 
AnswerRe: silverlight Pin
Pete O'Hanlon29-Jun-14 6:51
mvePete O'Hanlon29-Jun-14 6:51 
GeneralRe: silverlight Pin
Jammer30-Jun-14 23:32
Jammer30-Jun-14 23:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.