为什么结合影响身高? [英] Why does binding affect height?

查看:148
本文介绍了为什么结合影响身高?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在资源/ Shared.xaml样式定义(更新

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.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="../Resources/Shared.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

用户控制:

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

型号:

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

我用Caliburn.Micro如果它使任何区别。

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

结果:

为什么?

更新:一些Snooping之后,事实证明,第一个标签内的TextBlock有0利润率和价值来源是默认的,第二个是10和风格

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.

更新2:读了<经过href=\"http://stackoverflow.com/questions/9035878/implicit-styles-in-application-resources-vs-window-resources\">this 疑问的事实证明,定义的TextBlock 风格应该的的被应用到的TextBlocks 标签。因此,似乎对一个标签绑定存在在某种程度上改变。

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.

推荐答案

您必须拥有它影响一些其他的风格。

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>

通过与史努比乱搞,我可以通过改变的填充的复制图像边框对象,因此,请检查您的XAML,看看是否有修改填充你的<$ C $的任何隐含的风格C>边框标签

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

更新

添加您已经添加到您的问题额外样式之后,我能够重现您所得到的结果。

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

这个问题似乎是在含蓄风格的的TextBlock 被应用到的TextBlock 绑定内标签,但不与未结合的。

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.

我怀疑这是关系到一个事实,即<一个href=\"http://blogs.msdn.com/b/wpfsdk/archive/2009/08/27/implicit-styles-templates-controls-and-frameworkelements.aspx\"相对=nofollow>含蓄的风格并不意味着越过边界模板中,除非的元素从控制。 标签继承控制,但的TextBlock 没有。​​

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.

由于绑定到一个数值时,这只是发生,我最好的猜测是,它确定如何将绘制 Label.Content A小数进程标识父控件作为一个标签,而写了字符串 Label.Content 自动知道使用的TextBlock ,并且不适隐含的样式。

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.

这篇关于为什么结合影响身高?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆