0

DataTemplate在 aItemsControl里面UserControl。UserControl 在堆栈面板中被多次添加。(很少)

我需要能够确定堆栈面板有多少个孩子。我认为使用该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>

所以我基本上想知道有多少用户控件被添加到堆栈面板中,并且能够使用这个数量的孩子来计算 DataTemplate 内网格的高度。

FindAncestor 相对来源给我一个错误,说相对上下文不支持儿童。

4

1 回答 1

1

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

我已经尝试过这段代码并且它有效。只是为了展示我手动添加了两个用户控件。我还尝试将用户控件放在单独的 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();
    }
}

如果我仍然不明白这个问题,请告诉我:)

于 2015-04-14T15:07:49.667 回答