绑定到附加属性Grid.Row和Grid.Column

时间:2015-08-10 10:04:15

标签: c# wpf c#-4.0 datatemplate

我需要绑定到Grid.Row和Grid.Column等附加属性,但它似乎无法正常工作。 我尝试了多种选择,但没有运气,下面是xaml的详细信息。

 <Grid>
                <Grid>
                    <Grid vm:GridHelper.RowCount="{Binding RowCount}"
                          vm:GridHelper.ColumnCount="2" />
                    <ItemsControl ItemsSource="{Binding ControlCollection}">
                        <ItemsControl.ItemContainerStyle>
                            <Style>
                                <Setter Property="Grid.Row" Value="{Binding Path=Row}" />
                                <Setter Property="Grid.Column" Value="{Binding Path=Column}" />
                            </Style>
                        </ItemsControl.ItemContainerStyle>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Text}" Margin="5,5,20,5" FontSize="12" 
                                           TextWrapping="WrapWithOverflow"
                                           Foreground="#AEAEAE" FontFamily="Tahoma" VerticalAlignment="Center"/>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>

                    </ItemsControl>
                </Grid>

1 个答案:

答案 0 :(得分:2)

为了使ItemContainerStyle中的Grid.RowGrid.Column setter生效,您必须使用Grid作为ItemsControl的ItemsPanel

<ItemsControl ItemsSource="{Binding ControlCollection}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    ...
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    ...
                </Grid.ColumnDefinitions>
            </Grid>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    ...
</ItemsControl>