Why does binding affect height?
Asked Answered
B

1

4

Style definition in Resources/Shared.xaml (updated):

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:system="clr-namespace:System;assembly=mscorlib">
<system:Double x:Key="fullHeight" >26</system:Double>
<system:Double x:Key="halfHeight" >16</system:Double>
<Thickness x:Key="m">10</Thickness>
<Style TargetType="Button">
    <Setter Property="FontSize" Value="{StaticResource fullHeight}"/>
    <Setter Property="Margin" Value="{StaticResource m}"/>
    <Setter Property="Padding" Value="10"/>
</Style>
<Style TargetType="Label">
    <Setter Property="FontSize" Value="{StaticResource fullHeight}"/>
    <Setter Property="Margin" Value="{StaticResource m}"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
</Style>
<Style TargetType="TextBlock">
    <Setter Property="FontSize" Value="{StaticResource fullHeight}"/>
    <Setter Property="Margin" Value="{StaticResource m}"/>
</Style>
<Style TargetType="TextBox">
    <Setter Property="FontSize" Value="{StaticResource fullHeight}"/>
    <Setter Property="Margin" Value="{StaticResource m}"/>
    <Setter Property="Padding" Value="10"/>
</Style>
<Style TargetType="PasswordBox">
    <Setter Property="FontSize" Value="{StaticResource fullHeight}"/>
    <Setter Property="Margin" Value="{StaticResource m}"/>
    <Setter Property="Padding" Value="10"/>
</Style>
<Style TargetType="ListView">
    <Setter Property="FontSize" Value="{StaticResource fullHeight}"/>
    <Setter Property="Margin" Value="{StaticResource m}"/>
    <Setter Property="Padding" Value="10"/>
</Style>
<Style TargetType="ComboBox">
    <Setter Property="Margin" Value="{StaticResource m}"/>
</Style>
</ResourceDictionary>

Window:

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="../Resources/Shared.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

User control:

<StackPanel Orientation="Horizontal">
  <Label Content="Text" Background="AliceBlue"/>
  <Label Content="{Binding DecimalValue, FallbackValue=50}" Background="Aquamarine"/>
</StackPanel>

Model:

    private decimal _DecimalValue;
    public decimal DecimalValue
    {
        get { return _DecimalValue; }
        set
        {
            if (_DecimalValue != value)
            {
                _DecimalValue = value;
                NotifyOfPropertyChange();
            }
        }
    }

I'm using Caliburn.Micro if it makes any difference.

Result:

enter image description here

Why?

Update: After some Snooping, it turns out that the inner TextBlock of the first Label has margin of 0 and Value Source is Default and for the second it's 10 and Style.

Update 2: After reading up this question it turns out that defined TextBlock style should not be applied to TextBlocks inside Labels. So it seems that existence of binding on a Label somehow changes that.

Blastula answered 10/4, 2013 at 10:11 Comment(11)
Please show the definition of fullHeight and m.Butcherbird
Just tried this in VS2012/.NET4.5. Works fine in the designer and at runtime - both labels the same height. Which version of VS are you using?Misdirect
Have you tried these snippets stand alone? Maybe you have some other style that's getting applied?Beach
@Gaz I don't think so. Even if I had another style somewhere, what would discriminate between the two labels?Blastula
@Misdirect Same config as yours.Blastula
Clutching at straws - I have VS2012 update 2 installed.Misdirect
@user676571: Just wondered if you'd sanitised the xaml for posting on here and inadvertently removed something that might be affecting it (eg an id tag). I've done the same myself before now.Beach
@Gaz I'll give more context in a minute, but I'm not sure it will help.Blastula
Is it displayed wrong during runtime or during design time in VS?Iapetus
@Iapetus Image is taken from the runtime. In design time those styles do not apply as they are defined in the window.Blastula
It's working fine for me as well. If you create a completely new project and copy/paste the content found in this question there, do you experience the same problem? That should tell us if it's something to do with your application or your system.Rawls
R
1

You must have some other style affecting it.

My best guess would be check your Padding properties, because when I copy and paste your styles to a new project, the heights and margins are the same as your image, however the Padding is different.

Your Labels are actually getting rendered like this:

<Label>
    <Border>
        <ContentPresenter>
            <TextBlock />
        </ContentPresenter>
    </Border>
</Label>

By messing around with Snoop, I can duplicate your image by altering the Padding of the Border object, so check your XAML to see if you have any implicit styles that change the Padding of your Border tags

Update

After adding the extra styles you've added to your question, I am able to reproduce the results you are getting.

The problem appears to be that the implicit style for your TextBlock is being applied to the TextBlock inside the bound label, but not to the unbound one.

It should be noted this only happens when binding to a decimal value, not to a string.

I suspect this is related to the fact that implicit styles are not meant to cross template boundaries, unless the element inherits from Control. Label inherits from Control, however TextBlock does not.

Since this only happens when binding to a numeric value, my best guess is that the process that determines how to draw a Decimal for Label.Content identifies the parent control as a Label, while the process that writes a string to Label.Content automatically knows to use a TextBlock, and does not apply the implicit styles.

Rawls answered 10/4, 2013 at 14:23 Comment(10)
Snoop FTW. This tool is great. I was wondering if there is something that would enable style debugging. Anyway, it tells me that the inner TextBlock of the first Label has margin of 0 and Value Source is Default and for the second it's 10 and Style. What could be the cause? I should update the question to include whole Shared.xamlBlastula
@user676571 It's probably an implicit style somewhere that is only affecting one of your TextBlocks due to it's scope. Search your XAML for a style that specifies a type of TextBlock, but no x:Key, like this: <Style TargetType="{x:Type TextBlock}" />Rawls
Exactly. Question is, why is it not affectiong the other one?Blastula
@user676571 I'm not sure, is that your complete XAML? Your style can have a different scope depending on where it's located. For example, <Window.Resources> will apply to everything, while <UserControl.Resources> will only apply to that user control.Rawls
That is all there is. All implicit styles are defined in Shared.xaml.Blastula
@user676571 When you view your application in Snoop, do both Labels contain the exact same set of tags? (Border, ContentPresenter, and TextBlock)Rawls
Yes, they do. Structure is the same, the only difference is that from the first comment: the inner TextBlock of the first Label has margin of 0 and Value Source is Default and for the second it's 10 and Style.Blastula
Perhaps I am misreading your update, but in this case Label bound to a decimal has margin. To me it seems as if binding mechanism is applying implicit style across template boundary. On a side note, it seems odd that nobody has encountered this before. Are styles not meant to be used in this way?Blastula
@user676571 Sorry, I had that backwards and have updated it. I'm actually asking a different SO question about this since I'm quite curious what the actual logic is behind this :)Rawls
No problem. Let's hope your question will attract more attention :)Blastula

© 2022 - 2024 — McMap. All rights reserved.