如何摆脱恼人的Horizo​​ntalContentAlignment约束力的警告?约束力、恼人、Horizo、ntalContentAlignment

2023-09-04 02:06:18 作者:帅比柠七

我工作在一个大的WPF项目并调试我的输出窗口期间充满了这些恼人的警告:

I am working on a large WPF project and during debug my output window is filled with these annoying warnings:

System.Windows.Data信息:10:使用绑定和没有有效>回落值存在无法检索值;使用默认值代替。    BindingEx pression:路径= Horizo​​ntalContentAlignment; DataItem的= NULL; 目标元素是    ComboBoxItem (名称=''); target属性是'Horizo​​ntalContentAlignment(类型>   的Horizo​​ntalAlignment')

System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid > fallback value exists; using default instead. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; target element is 'ComboBoxItem' (Name=''); target property is 'HorizontalContentAlignment' (type >' HorizontalAlignment')

在该特定实例ComboBoxItem的样式是这样的:

In the specific example ComboBoxItem is styled in this way:

<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 ,它包含的东西,如:

I know that the problem is generated by the default theme definition for ComboBoxItem that contains things like:

<Setter Property="Control.HorizontalContentAlignment">
        <Setter.Value>
            <Binding Path="HorizontalContentAlignment" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ItemsControl, AncestorLevel=1}" />
            </Setter.Value>
        </Setter>

但我也想到用

but I also thought that using

<Setter Property="OverridesDefaultStyle" Value="True"/> 

将采取照顾的问题,而是警告仍然存在。

would taken care of the problem, and instead warnings are still there.

编辑:为了再现您还需要重写组合框的风格在这个例子中,从MSDN酷似完成的问题: 组合框控件模板实例

In order to reproduce the problem you need to override also the style of ComboBox exactly like done in this example from MSDN: ComboBox ControlTemplate Example

任何帮助,真是AP preciated

Any help is really appreciated

推荐答案

这为我工作。把这个在您的Application.xaml文件。

This worked for me. Put this in your Application.xaml file.

<Application.Resources>
    <Style TargetType="ComboBoxItem">
        <Setter Property="HorizontalContentAlignment" Value="Left" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
    </Style>
</Application.Resources>

这是...

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/42cd1554-de7a-473b-b977-ddbd6298b3d0