相关疑难解决方法(0)

如何摆脱恼人的Horizo​​ntalContentAlignment绑定警告?

我正在研究一个大型的WPF项目,在调试期间,我的输出窗口充满了这些烦人的警告:

System.Windows.Data信息:10:无法使用绑定检索值,并且不存在有效>回退值; 使用默认值. BindingExpression:路径= Horizo​​ntalContentAlignment; 的DataItem = NULL; target元素是' ComboBoxItem '(Name =''); target属性是'Horizo​​ntalContentAlignment'(类型>'Horizo​​ntalAlignment')

在具体示例中,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>
Run Code Online (Sandbox Code Playgroud)

我知道问题是由ComboBoxItem的默认主题定义生成的,其中包含以下内容:

<Setter Property="Control.HorizontalContentAlignment">
        <Setter.Value>
            <Binding Path="HorizontalContentAlignment" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ItemsControl, AncestorLevel=1}" />
            </Setter.Value>
        </Setter>
Run Code Online (Sandbox Code Playgroud)

但我也认为使用

<Setter Property="OverridesDefaultStyle" …
Run Code Online (Sandbox Code Playgroud)

.net wpf xaml binding

15
推荐指数
2
解决办法
5667
查看次数

标签 统计

.net ×1

binding ×1

wpf ×1

xaml ×1