【发布时间】:2011-02-09 14:54:07
【问题描述】:
我正在处理一个大型 WPF 项目,在调试期间,我的输出窗口充满了这些烦人的警告:
System.Windows.Data 信息:10:无法使用绑定检索值,并且不存在有效的 > 后备值;改用默认值。 BindingExpression:Path=HorizontalContentAlignment; DataItem=null; 目标元素是 'ComboBoxItem' (Name='');目标属性是'HorizontalContentAlignment'(类型>' 水平对齐')
在具体示例中,ComboBoxItem 的样式是这样的:
<Style x:Key="{x:Type ComboBoxItem}" TargetType="{x:Type ComboBoxItem}">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBoxItem}">
<Border
Name="bd"
Padding="4,4,4,4"
SnapsToDevicePixels="True"
CornerRadius="2,2,2,2">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsHighlighted" Value="true">
<Setter TargetName="bd" Property="Background" Value="{StaticResource MediumBrush}"/>
<Setter TargetName="bd" Property="Padding" Value="4,4,4,4"/>
<Setter TargetName="bd" Property="CornerRadius" Value="2,2,2,2"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我知道问题是由 ComboBoxItem 的默认主题定义产生的,其中包含以下内容:
<Setter Property="Control.HorizontalContentAlignment">
<Setter.Value>
<Binding Path="HorizontalContentAlignment" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ItemsControl, AncestorLevel=1}" />
</Setter.Value>
</Setter>
但我也认为使用
<Setter Property="OverridesDefaultStyle" Value="True"/>
会解决问题,但警告仍然存在。
编辑:为了重现问题,您还需要重写 ComboBox 的样式,就像在 MSDN 的这个示例中所做的那样: ComboBox ControlTemplate Example
非常感谢任何帮助
【问题讨论】:
-
我无法在 4.0 和 3.5 中重现此 XAML 的问题。它运行良好,没有任何绑定警告。
-
你是对的,我单独测试了,它没有给我警告,我编辑问题以获取更多详细信息
-
我在您在编辑中链接的示例中看不到有问题的绑定。
-
要查看此类警告,您必须设置出现的消息级别。如果设置为全部,那么这很常见。这可以在 app.config 文件中更改。