【问题标题】:Why does binding affect height?为什么绑定会影响高度?
【发布时间】:2013-04-10 10:11:35
【问题描述】:

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。

结果:

为什么?

更新:经过一番窥探,发现第一个 Label 的内部 TextBlock 的边距为 0,Value Source 为 Default,第二个为 10,Style。

更新 2: 阅读 this question 后发现,定义的 TextBlock 样式应该应用于Labels 内的TextBlocks。因此,Label 上的绑定似乎以某种方式改变了这一点。

【问题讨论】:

  • 请给出fullHeightm的定义。
  • 刚刚在VS2012/.NET4.5中试过这个。在设计器和运行时工作正常 - 两个标签的高度相同。你用的是哪个版本的VS?
  • 你试过这些独立的sn-ps吗?也许您还有其他正在应用的样式?
  • @Gaz 我不这么认为。即使我在某个地方有另一种风格,这两个标签之间的区别是什么?
  • @Phil 和你的配置一样。

标签: wpf binding styles caliburn.micro


【解决方案1】:

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

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

您的标签实际上是这样呈现的:

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

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

更新

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

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

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

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

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

【讨论】:

  • 窥探 FTW。这个工具很棒。我想知道是否有一些东西可以启用样式调试。无论如何,它告诉我第一个Label 的内部TextBlock 的边距为0,值源为默认值,第二个为10 和样式。可能是什么原因?我应该更新问题以包括整个 Shared.xaml
  • @user676571 这可能是某处的隐式样式,由于其范围,它只会影响您的一个 TextBlocks。在 XAML 中搜索指定类型为 TextBlock 但不指定 x:Key 的样式,如下所示:&lt;Style TargetType="{x:Type TextBlock}" /&gt;
  • 没错。问题是,为什么它不影响另一个?
  • @user676571 我不确定,那是完整的 XAML 吗?您的样式可能有不同的范围,具体取决于它所在的位置。例如,&lt;Window.Resources&gt; 将适用于所有内容,而 &lt;UserControl.Resources&gt; 将仅适用于该用户控件。
  • 就是这样。所有隐式样式都在Shared.xaml 中定义。
猜你喜欢
  • 2013-07-02
  • 1970-01-01
  • 1970-01-01
  • 2019-08-09
  • 1970-01-01
  • 2013-05-19
  • 1970-01-01
  • 2014-11-25
  • 2018-10-26
相关资源
最近更新 更多