gpt4 book ai didi

wpf - 为什么绑定(bind)会影响高度?

转载 作者:行者123 更新时间:2023-12-04 21:43:37 27 4
gpt4 key购买 nike

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

为什么?

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

更新 2:看完 this question原来定义 TextBlock样式不应应用于 TextBlocks里面 Labels .所以似乎 Label 上存在绑定(bind)以某种方式改变了这一点。

最佳答案

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

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

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

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

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

更新

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

问题似乎是您的 TextBlock 的隐式样式正在应用于 TextBlock在绑定(bind)的标签内,但不在未绑定(bind)的标签内。

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

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

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

关于wpf - 为什么绑定(bind)会影响高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15922964/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com