Alyanto
asked on
Inherited DependencyProperty
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?
This is what fails in the code behind
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"
<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"/>
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);
}
This is what fails in the code behind
case "Label":
Label lbl = (Label)child;
lbl.Height = lbl.ItemWidth * HeightChangePercentage;
lbl.Width = lbl.ItemHeight * WidthChangePercentage;
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It works
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:
Open in new window
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.