21

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

Drake
  • 8,225
  • 15
  • 71
  • 104
  • I cannot reproduce your problem with this XAML neither in 4.0 nor in 3.5. It runs fine without any binding warnings. – majocha Apr 19 '10 at 19:11
  • you are right, I tested alone and it doesn't give me warning, I edit the question for more details – Drake Apr 20 '10 at 09:25
  • I cannot see that problematic binding in the example you linked in edit. – majocha Apr 20 '10 at 12:09
  • To see that kind of warnings you have to set the level of messages that appear. If set to all then this is usual. This can be changed in app.config file. – kubal5003 Jun 07 '10 at 00:50

4 Answers4

18

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

Carter Medlin
  • 11,857
  • 5
  • 62
  • 68
  • 1
    In case I'm not the only one who thought the style could go in any of the usual places - like the element itself, or its parent window/user control, etc - and failed to fix it, I want to point out that for some reason unknown to me, it only works in Application.Resources (as shown in this answer). – andrew Mar 29 '20 at 07:30
  • andrew's comment is right!! I had the same problem with ComboBoxes ... Too bad that I noticed the comment too late and only after having put those lines in a ResourceDictionary, in the ComboBox, in , etc... – Andrea Antonangeli Jun 11 '20 at 17:49
1

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.

Gabe Miller
  • 132
  • 2
  • 10
0

I just want to mention I struggled with a similar problem for two days (mine was a "Windows Data Error 4" error, complaining about HorizontalContentAlignment and VerticalContentAlignment).

The most common suggested solution (adding the Horizontal/VerticalContentAlignment Style to your element, or even to the App.xaml) does NOT always solve the problem.

Eventually, I discovered something unique to my own situation - I hope it can be of help to someone: If you are using the FilterEventHandler, don't unsubscribe it before resubscribing!

My old code kept on generating that "Data Error 4" message whenever I changed the Channel Filter (which calls UpdateCorporatesList):

// This code generates errors
private void UpdateCorporatesList()
{
    this.CorporatesViewSource.Filter -= new FilterEventHandler(ApplyCorporateFilter);

    if (this.ChannelFilter != null)
    {
        this.CorporatesViewSource.Filter += new FilterEventHandler(ApplyCorporateFilter);
    }
    else
    {
        this.CorporateFilter = null;
    }
}

private void ApplyCorporateFilter(object sender, FilterEventArgs e)
{
    SalesCorporate customer = e.Item as SalesCorporate;
    var currentChannel = this.Channels.FirstOrDefault(x => x.ID == this.ChannelFilter).Description;
    if ((customer.ID != null) && (customer.Channel != currentChannel))
    {
        e.Accepted = false;
    }
}

...so I changed it to re-subscribe to the FilterEventHandler every time, and rather put the check for a null on Channel Filter in the event-handling method.

// This code works as intended
private void UpdateCorporatesList()
{
    this.CorporatesViewSource.Filter += new FilterEventHandler(ApplyCorporateFilter);

    if (this.ChannelFilter == null)
    {
        this.CorporateFilter = null;
    }
}

private void ApplyCorporateFilter(object sender, FilterEventArgs e)
{
    var currentChannel = this.Channels.FirstOrDefault(x => x.ID == this.ChannelFilter);
    if (currentChannel.ID == null)
    {
        return;
    }

    SalesCorporate customer = e.Item as SalesCorporate;
    if ((customer.ID != null) && (customer.Channel != currentChannel.Description))
    {
        e.Accepted = false;
    }
}

Et Voila! No more errors :-)

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Riegardt Steyn
  • 5,431
  • 2
  • 34
  • 49
0

I can't set style in App.Resources (because of the way program is built) and other solutions didn't work for me. However, I found the other way:

  • Never set combobox source to null
  • Instead when refreshing clear the collection and add items to it one by one
  • Make sure to use ObservableCollection instead of List keep your UI updated