Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get rid of annoying HorizontalContentAlignment binding warning?

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

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')

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>

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.

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

Any help is really appreciated

like image 539
Drake Avatar asked Apr 19 '10 10:04

Drake


2 Answers

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>

from...

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

like image 101
Carter Medlin Avatar answered Nov 17 '22 02:11

Carter Medlin


I don't know if after more than a year your are still interested in this problem but my solution was to explicitly write in the style the value for this. For example:

<Style x:Key="{x:Type ComboBoxItem}" TargetType="{x:Type ComboBoxItem}">
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>

And that simply solved that problem.

like image 27
Gabe Miller Avatar answered Nov 17 '22 03:11

Gabe Miller