WPF绑定Grid.Column属性

时间:2011-03-24 12:34:44

标签: wpf data-binding grid datatemplate itemscontrol

我需要通过转换器设置项目的Grid.Column属性。 这是我的xaml:

<ItemsControl.ItemTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding Periodo.Inizio}">
            <Grid.Column>
                <MultiBinding Converter="{StaticResource ItemColumnSetter}">
                    <Binding RelativeSource="{RelativeSource Self}" />
                    <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=local:Timeline}" Path="StartDate" />
                    <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=local:Timeline}" Path="EndDate" />
                    <Binding Path="Periodo.Inizio" />
                </MultiBinding>
            </Grid.Column>
        </TextBlock>
    </DataTemplate>
</ItemsControl.ItemTemplate>

但是不要工作。 我确信转换器运行良好......

1 个答案:

答案 0 :(得分:4)

您的TextBlock将被包装在某种控件中,这意味着任何Grid.XXX属性都将被忽略。要正确应用这些,您需要在ItemsControl.ItemContainerStyle

中进行绑定

应该是这样的:

<ItemsControl.ItemContainerStyle>
    <Style>
        <Setter Property="Grid.Column">
            <Setter.Value>
                <MultiBinding Converter="{StaticResource ItemColumnSetter}">
                    <Binding RelativeSource="{RelativeSource Self}" />
                    <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=local:Timeline}" Path="StartDate" />
                    <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=local:Timeline}" Path="EndDate" />
                    <Binding Path="Periodo.Inizio" />
                </MultiBinding>
            </Setter.Value>
        </Setter>
    </Style>
</ItemsControl.ItemContainerStyle>