简体   繁体   中英

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?

如果需要绑定Parameter ,则通常应使用MultiBinding

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM