【问题标题】:Binding to attached property Grid.Row and Grid.Column绑定到附加属性 Grid.Row 和 Grid.Column
【发布时间】:2015-08-10 10:04:15
【问题描述】:

我需要绑定到附加属性,如 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>

【问题讨论】:

    标签: c# wpf c#-4.0 datatemplate


    【解决方案1】:

    为了使 ItemContainerStyle 中的 Grid.RowGrid.Column 设置器有效,您必须使用 Grid 作为 ItemsControl 的 ItemsPanel

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

    【讨论】:

      猜你喜欢
      • 2012-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-07
      相关资源
      最近更新 更多