Mark Heath
Mark Heath

Reputation: 49522

Use RelativeSource as a ConverterParameter in a style

I am trying to use a converter in an items control ItemContainerStyle to convert a property of an item in the ItemsControl to an X value. To do the conversion, I also need a reference to the parent UserControl that the ItemsControl is contained in. I tried to use RelativeSource, but in the Convert function, the parameter is not a UserControl but an instance of RelativeSource. Here's my XAML:

<ItemsControl x:Name="itemsControl">
    <ItemsControl.ItemContainerStyle>
        <Style>
            <Setter Property="Canvas.Left">
                <Setter.Value>
                    <Binding Path="StartTime" 
                             Converter="{StaticResource startTimeToXConverter}"
                             ConverterParameter="{RelativeSource Mode=FindAncestor, AncestorType=UserControl}" />
                </Setter.Value>
            </Setter>
            <Setter Property="Canvas.Top" Value="{Binding Path=Y}" />
        </Style>
    </ItemsControl.ItemContainerStyle>

Is there something wrong with my syntax, or is there another way of achieving this?

Upvotes: 2

Views: 4525

Answers (1)

brunnerh
brunnerh

Reputation: 185553

If you need to bind the Parameter you should usually use a MultiBinding instead.

Upvotes: 3

Related Questions