如何在DataTemplate中设置网格的高度

时间:2015-04-14 14:31:04

标签: c# wpf user-controls relativesource

DataTemplate用于ItemsControl内的UserControl。 UserControl在stackpanel中多次添加。 (pfew)

我需要能够确定stackpanel有多少个孩子。我认为这可以使用FindAncestor模式,但我担心我需要你的帮助。

这是XAML逻辑:

<StackPanel Name="BeforeTournament" Orientation="Horizontal" VerticalAlignment="Top">
    <UserControl ...
    <Grid>
        <TextBlock Name="txtTitle" FontSize="14" />
            <ItemsControl Name="MatchList" ItemsSource="{Binding Matches, Mode=OneWay}" Width="400" Margin="-7,20,0,0"
                  ScrollViewer.HorizontalScrollBarVisibility="Hidden"
                  ScrollViewer.VerticalScrollBarVisibility="Hidden">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid Name="MatchTemplate" Width="390"
                          Height="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type StackPanel}},
                                       Path=(Children.Count * 300}"
                          Margin="0,0,0,50" VerticalAlignment="Center">
                    </Grid>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
    </UserControl>

    //Duplicates below, same logic to determine width
    <UserControl></UserControl>
</StackPanel>

所以我基本上想知道有多少UserControl已添加到stackpanel并且能够使用这些子项来计算DataTemplate中网格的高度。

FindAncestor相对来源给我一个错误,说明在相对上下文中不支持Children。

1 个答案:

答案 0 :(得分:1)

好吧,正如我在评论中所说,应该有更好的方法来做到这一点,但我非常确定一种方法是使用转换器。将stackpanel作为参数传递,并返回子项数乘以300(如果这是你想要的)

我已经尝试过这段代码,但它确实有用。仅为show我手动添加了两个usercontrols。我还尝试将usercontrols放在一个单独的xaml文件中。

Main.xaml

<Window.Resources>
    <local:StackpanelConverter x:Key="StackpanelConverter"/>
</Window.Resources>
<StackPanel Name="BeforeTournament" Orientation="Horizontal" VerticalAlignment="Top">
    <UserControl>
        <Grid Height="200" Background="Brown">
            <TextBlock Name="txtTitle" FontSize="14" />
            <ItemsControl Name="MatchList" ItemsSource="{Binding MyControls}" BorderBrush="Bisque" BorderThickness="10"  Width="400" Margin="-7,20,0,0"
              ScrollViewer.HorizontalScrollBarVisibility="Hidden"
              ScrollViewer.VerticalScrollBarVisibility="Hidden">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid Name="MatchTemplate" Width="390" Background="Blue"
                   Height="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type StackPanel}}, Converter={StaticResource StackpanelConverter}}"
                      Margin="0,0,0,50" VerticalAlignment="Center">

                        </Grid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </Grid>
    </UserControl>
    <UserControl>
        <Grid Height="200" Background="Brown">
            <TextBlock  FontSize="14" />
            <ItemsControl ItemsSource="{Binding MyControls}" BorderBrush="Bisque" BorderThickness="10"  Width="400" Margin="-7,20,0,0"
              ScrollViewer.HorizontalScrollBarVisibility="Hidden"
              ScrollViewer.VerticalScrollBarVisibility="Hidden">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid Name="MatchTemplate" Width="390" Background="Blue"
                   Height="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type StackPanel}}, Converter={StaticResource StackpanelConverter}}"
                      Margin="0,0,0,50" VerticalAlignment="Center">

                        </Grid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </Grid>
    </UserControl>
</StackPanel>

转换器示例:(这是用记事本写的,因此可能存在错误)

 public class StackpanelConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var stackpanel = value as StackPanel;
        var height = stackpanel.Children.Count;
        return height*300;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

告诉我,如果我仍然不理解这个问题:)