为什么绑定会影响身高?

时间:2013-04-10 10:11:36

标签: wpf binding styles caliburn.micro

Resources / Shared.xaml中的样式定义(已更新):

<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。

结果:

enter image description here

为什么?

更新:在一些窥探后,事实证明第一个Label的内部TextBlock的边距为0,而Value Source为Default,第二个的内部TextBlock为10和Style。

更新2 :在阅读this question之后,事实证明定义的TextBlock样式应该应用于TextBlocks里面Labels。所以似乎Label上存在绑定会以某种方式改变它。

1 个答案:

答案 0 :(得分:1)

你必须有其他影响它的风格。

我最好的猜测是检查您的Padding属性,因为当我将样式复制并粘贴到新项目时,高度和边距与您的图像相同,但Padding不同。

您的标签实际上是这样渲染的:

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

通过弄乱Snoop,我可以通过更改Border对象的填充来复制您的图像,因此请检查您的XAML以查看是否有任何隐式样式可以更改Padding您的Border代码

<强>更新

添加您添加到问题中的额外样式后,我可以重现您获得的结果。

问题似乎是TextBlock的隐式样式被应用于绑定标签内的TextBlock,而不应用于未绑定的标签。

应该注意,这仅在绑定到十进制值时发生,而不是绑定到字符串。

我怀疑这与implicit styles are not meant to cross template boundaries除非元素继承自Control这一事实有关。 Label继承自Control,但TextBlock没有。

由于这仅在绑定到数值时发生,我最好的猜测是确定如何为Label.Content绘制十进制的过程将父控件标识为Label,而过程是将string写入Label.Content会自动知道使用TextBlock,并且不会应用隐式样式。

相关问题