4

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。

结果:

在此处输入图像描述

为什么?

更新:经过一番窥探,结果发现第一个标签的内部 TextBlock 的边距为 0,值源为默认值,第二个为 10 和样式。

更新 2:在阅读了这个问题之后,事实证明定义的TextBlock样式应该应用于TextBlocksinside Labels。所以似乎绑定的存在以Label某种方式改变了这一点。

4

1 回答 1

1

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

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

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

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

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

更新

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

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

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

我怀疑这与隐式样式并不意味着跨越模板边界这一事实有关,除非元素继承自Control. Label继承自Control,但TextBlock不继承。

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

于 2013-04-10T14:23:15.020 回答