ItemsControl with DataTemplate in code behind 只显示一个项目

ItemsControl with DataTemplate in code behind only displays one item

我正在尝试从后面的代码创建一个 DataTemplate,但在 ItemsControl 中使用它,只显示了一项 ...

如果我在XAML中描述DataTemplate,我拥有所有项目。

我使用此处描述的代码创建我的 DataTemplate

完整XAML:

<ItemsControl ItemsSource="{Binding Radios}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid Height="40">
                    <TextBlock Text="{Binding Name}" TextTrimming="CharacterEllipsis" Margin="6" HorizontalAlignment="Left" VerticalAlignment="Center" />
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

这是我背后的代码:

        var grid = new Grid();
        grid.Height = 40;

        var textblock = new TextBlock();
        textblock.SetBinding(TextBlock.TextProperty, "Name");
        textblock.Margin = new Thickness(6);
        textblock.TextTrimming = TextTrimming.CharacterEllipsis;
        textblock.HorizontalAlignment = HorizontalAlignment.Left;
        textblock.VerticalAlignment = VerticalAlignment.Center;

        grid.Children.Add(textblock);

        RadioDataTemplate = TemplateGenerator.CreateDataTemplate(() => grid);

  <ItemsControl ItemsSource="{Binding Radios}" ItemTemplate="{Binding RadioDataTemplate}" />

编辑:

SharedSizeGroup

有问题
RadioDataTemplate = TemplateGenerator.CreateDataTemplate(() =>
        {
            var grid = new Grid();
            grid.Height = 40;

            for (int i = 1; i <= 2; i++)
            {
                grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });

                if (i < 2)
                    grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Auto), SharedSizeGroup = "A" });
            }

            var image = new Image();
            image.SetBinding(Image.SourceProperty, "Image");
            image.Stretch = System.Windows.Media.Stretch.Uniform;
            RenderOptions.SetBitmapScalingMode(image, BitmapScalingMode.HighQuality);
            image.HorizontalAlignment = HorizontalAlignment.Center;
            image.VerticalAlignment = VerticalAlignment.Center;

            grid.Children.Add(image);
            Grid.SetColumn(image, 0);

            var gridsplitter = new GridSplitter();
            gridsplitter.HorizontalAlignment = HorizontalAlignment.Stretch;
            gridsplitter.Width = 5;
            gridsplitter.Background = new SolidColorBrush(Colors.Red);

            grid.Children.Add(gridsplitter);
            Grid.SetColumn(gridsplitter, 1);

            var textblock = new TextBlock();
            textblock.SetBinding(TextBlock.TextProperty, "Name");
            textblock.Margin = new Thickness(6);
            textblock.TextTrimming = TextTrimming.CharacterEllipsis;
            textblock.HorizontalAlignment = HorizontalAlignment.Left;
            textblock.VerticalAlignment = VerticalAlignment.Center;

            grid.Children.Add(textblock);
            Grid.SetColumn(textblock, 2);

            return grid;
        });

不清楚您为什么要这样做,至少在代码隐藏中创建 DataTemplate 是完全不寻常的。无论如何,

() => grid

returns 始终只有您之前创建的同一个 Grid 实例。

但是,您必须在每次调用时创建一个新的:

RadioDataTemplate = TemplateGenerator.CreateDataTemplate(() =>
{
    var grid = new Grid();
    grid.Height = 40;

    var textblock = new TextBlock();
    textblock.SetBinding(TextBlock.TextProperty, "Name");
    textblock.Margin = new Thickness(6);
    textblock.TextTrimming = TextTrimming.CharacterEllipsis;
    textblock.HorizontalAlignment = HorizontalAlignment.Left;
    textblock.VerticalAlignment = VerticalAlignment.Center;

    grid.Children.Add(textblock);
    return grid;
});