Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Inherited DependencyProperty

Posted on 2016-11-02
4
Medium Priority
?
50 Views
Last Modified: 2016-11-08
I have created a user control based upon Panel and added a DependencyProperty called ItemWidth (double).  When I instantiate add the control in XML the DependencyProperty works as expected and accepts values.  When I process the forms collection I can see ItemWidth within the properties of the control after I cast it to its specific type in this case label.  so Label lbl = (Label)child.  Child being a UIElement.

But I do not seem to be able to access the ItemWidth primarily because it is not part of Label.  How to do this?

 xmlns:xpanels="clr-namespace:SABER.Performance.Windows.Controls.V3.XPanel"

Open in new window


            <Label x:Name="lblConsultant" 
                   Content="Bobb Smith" 
                   FontFamily="{Binding IFontFontFamily}"
                   FontSize="{Binding FontSize}" 
                   FontWeight="Bold"
                   Height="{Binding ConsultantHeight}"
                   Width="{Binding ConsultantWidth}"
                   xpanels:XPanel.RelativeX="{Binding ConsultantRelativeX}"
                   xpanels:XPanel.ItemHorizontalPositionAlignment="{Binding ConsultantHorizontal}"
                   xpanels:XPanel.RelativeY="{Binding ConsultantRelativeY}"
                   xpanels:XPanel.ItemVerticalPositionAlignment="{Binding ConsultantVertical}"
                   xpanels:XPanel.ItemCanResize="True"
                   xpanels:XPanel.ItemHeight="99.9"
                   xpanels:XPanel.ItemWidth="88.8"/>

Open in new window


        public static readonly DependencyProperty ItemWidthProperty =
            DependencyProperty.RegisterAttached("ItemWidth", typeof(double), typeof(XPanel), new PropertyMetadata(100.0, InvalidateLayoutCallback));

        public static double GetItemWidth(DependencyObject obj)
        {
            return (double)obj.GetValue(ItemWidthProperty);
        }

        public static void SetItemWidth(DependencyObject obj, double value)
        {
            obj.SetValue(ItemWidthProperty, value);
        }

Open in new window


This is what fails in the code behind

                            case "Label":
                                Label lbl = (Label)child;
                                lbl.Height = lbl.ItemWidth * HeightChangePercentage;
                                lbl.Width = lbl.ItemHeight * WidthChangePercentage;

Open in new window

0
Comment
Question by:Alyanto
  • 3
4 Comments
 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 41871085
In my opinion, one of two things is happening here:

1. You're not casting the label to the "correct" label type. I have no idea what that type should be, but it would have the .Height and .Width as assignable assessors.

2. You're right, the label doesn't have a .Height and .Width, so you would likely need to set the height and width like the following:

    <div>
        <label id="lbl" style="height: 20px; width:30px;"  runat="server">A Label Was Put Here</label>
    </div>

Open in new window


            const string myStyle = "height:{0}; width:{1};";
            const string HEIGHT = "height";
            const string WIDTH = "width";
            const int percent = 2;
            const string PX = "px";

            string styleAttribute = lbl.Attributes["style"];

            int height = 10;
            int width = 50;
            string heightValue = "10";
            string widthValue = "50";

            if (!string.IsNullOrEmpty(styleAttribute))
            {
                if (lbl.Attributes != null && lbl.Attributes["style"] != null)
                {
                    CssStyleCollection cssStyles = lbl.Attributes.CssStyle;

                    heightValue = cssStyles[HEIGHT];
                    widthValue = cssStyles[WIDTH];

                    if (!string.IsNullOrEmpty(heightValue))
                    {
                        heightValue = heightValue.Substring(0, heightValue.Length - PX.Length);
                        if (int.TryParse(heightValue, out height))
                        {
                            height = height * percent;
                        }
                    }

                    if (!string.IsNullOrEmpty(widthValue))
                    {
                        widthValue = widthValue.Substring(0, widthValue.Length - PX.Length);
                        if (int.TryParse(widthValue, out width))
                        {
                            width = width * percent;
                        }
                    }
                }
            }

            string styleString = string.Format(myStyle, height, width);
            lbl.Attributes["style"] = myStyle;

Open in new window


Note I'm giving you a possible generalized way of setting the style. You can fool around with the object in the debugger to ensure you have the correct. You might be able to get the attributes by just iterating through them too.
0
 
LVL 1

Author Comment

by:Alyanto
ID: 41871762
Hi Danial
The issue is with the  the access to ItemWidth and ItemHeight.  Width and Height give NaN and I can test for Not a Number (double.IsNotaNumber(x)).  The issue is that the Label or for that matter any control on the form is inheriting / implementing the DependencyProperty ItemWidth and ItemHeight much as Grid.Column or Grid.Row might be implemented in control hosted in a grid.

/Aly
0
 
LVL 1

Accepted Solution

by:
Alyanto earned 0 total points
ID: 41871832
I managed to solve the issue myself.

                            case "Label":
                                Label lbl = (Label)child;
                                lbl.Height = GetItemHeight(child) * HeightChangePercentage;
                                lbl.Width = GetItemWidth(child) * WidthChangePercentage;
                                break;

Open in new window

0
 
LVL 1

Author Closing Comment

by:Alyanto
ID: 41878488
It works
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month20 days, 23 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question