none
Why does the resource dictionary defined Height member not act in the expected fashion? RRS feed

  • Question

  • A WPF UserControl has been defined. Because this control is used in a dozen places over many UserControls and there are many attributes to be set a ResourceDictionary file was defined. One of the Setters in the Style defined a default value for the Height of the control. When this default is allowed to flow through then the control is a great deal taller than expected. When the same value is assigned locally, however, then the expected height is applied to the control.

    Here's what that looks like. The two controls are hosted in a Grid. The height of the row is set to "Auto". The control on the left is using the Height specified in the ResourceDictionary and it has caused the row to grow to its current height. The control on the right, as can be seen from the bounding rectangles, is shorter than the Grid's row.

    The control on the left's Height comes from the ResourceDictionary which which is defined in the manner shown below. 

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:uc="clr-namespace:CynoCommon.UserControls" xmlns:rotMod="clr-namespace:CynoCommon.Models" > <Style x:Key="LeftSideDial" TargetType="uc:RoCntrl"> <Setter Property="Height" Value="200" /> <Setter Property="Width" Value="125"/> </Style> </ResourceDictionary>

    The control on the right has its height specified locally as shown in the following manner. The actual UserControl class is RotCntrl. Both controls are given the Style defined in the ResourceDictionary. The only difference between the two is the fact that the left control's height comes from the ResourceDictionary while the one on the right is explicitly specified. However, the two values are supposed to be the same.

            <view:RotCntrl
                Grid.Row="3" Grid.Column="0" 
                Style="{StaticResource LeftSideDial}"
                />
    
            <view:RotCntrl 
                Grid.Row="3" Grid.Column="1" 
                Height="200"
                Style="{StaticResource LeftSideDial}"
                />
    
    So, what have I done wrong?


    Richard Lewis Haggard

    Friday, October 4, 2019 10:19 PM

Answers

  • Hi Richard.Haggard,
    The problem is still the Precedence-dependent priority problem. If the Height property in RotCntrl control in the UI is set as A, the Height property of UserControl is set as B, and the Height property in ResourceDictionary  is set as C, then the priority order of the them is A>B>C.
    Best regards
    Daisy Tian

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 8, 2019 5:32 AM

All replies

  • Hi Richard.Haggard,

    >Why does the resource dictionary defined Height member not act in the expected fashion?

    What you mean is when the right RotCntrl's Height is set to other value like 100, the Height defined in resource dictionary doesn't work. right? If I don't misunderstand, the Height’s precedence in right RotCntrl is higher than the Height defined in resource dictionary. In this official document Dependency Property Setting Precedence List, you could see the Style triggers is lower than Local value. If I misunderstand your question, please point out and give me a clearer description of the question.

    Best regards

    Daisy  Tian


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 7, 2019 9:46 AM
  • Sorry if the question was unclear. The question is why does the same height value defined in the resource directory height act differently from from the local value? 

    Richard Lewis Haggard

    Monday, October 7, 2019 2:19 PM
  • Both are set to 200. They look the same size and they're in the same row.  So you seem to have the same height.

    What difference are you seeing?


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Monday, October 7, 2019 8:38 PM
    Moderator
  • Ah. I think I understand the confusion. No, they are not the same height. The control itself draws itself at a particular size and does not support dynamic resizing so it is understandable that a person might think that they are the same height on first glance. However, the two examples are not the same height. The one on the left occupies a Grid row whose height is set to auto. The vertical extents of that control cause the row to expand far beyond the 200 specified in the ResourceDictionary. The example on the right, as can be seen from its outline, is actually 200 units high and, as can be seen from the outline, the extents of the control are reasonable. Because the two controls are on the same row in the Grid the left hand control with its absurdly large request for vertical space has expanded the row's height. The one on the right is floating within the middle of that row's height. Adding the upper and lower space with the actual size of the control (125.5 + 200 + 125.5) gives a row height of 451 instead of the expected 200.

    By the way, the  height of 451 comes from the UserControl's own XAML file which sets the DesignWidth to 800 and the Height to 451. The actual question can be restated as "Why does the ResourceDictionary Height attribute (<Setter Property="Height" Value="200"/>) not override the UserControl.XAML Height of 451 while a local attribute (Height=200) does?


    Richard Lewis Haggard


    Monday, October 7, 2019 8:48 PM
  • I removed the Height=451 from the UserControl.xaml file and now the ResourceDictionary's own <Setter Property="Height" Value="200"/> is reflected in the application's various Views. 

    The question is now modified: "Why does a Height in a UserControl's xaml file override a ResourceDictionary's override of that value but a local override at the point where the UserControl is inserted into a View remain effective?" It would seem that both the ResourceDictionary and local override would both be equally effective in replacing the UserControl's attributes.


    Richard Lewis Haggard

    Monday, October 7, 2019 9:19 PM
  • Hi Richard.Haggard,
    The problem is still the Precedence-dependent priority problem. If the Height property in RotCntrl control in the UI is set as A, the Height property of UserControl is set as B, and the Height property in ResourceDictionary  is set as C, then the priority order of the them is A>B>C.
    Best regards
    Daisy Tian

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 8, 2019 5:32 AM
  • Contrary to what one would expect, a Style in a ResourceDictionary is overwritten by the original value in the WPF UserControl. This arrangement makes a ResourceDictionary utterly useless but, what the heck, experience agrees with your statement. Thank you.

    Richard Lewis Haggard

    Sunday, October 13, 2019 8:39 PM