简体   繁体   English

WPF ItemsControl元素-根据窗口大小调整内容大小(锚定)

[英]WPF ItemsControl elements - Resize content based on the window size (Anchoring)

im working with wpf and i have the problem that i want to bind a collection to an element which resize its content based on the window size. 我正在使用wpf,我有一个问题,我想将一个集合绑定到一个元素,该元素根据窗口大小调整其内容的大小。

To make it more clear a little example: With static behaviour i would do something like that. 为了更清楚地说明一个例子:对于静态行为,我会做类似的事情。

<Grid Margin="10,10,10,10">
    <Grid.RowDefinitions>
        <RowDefinition Height="50*"/>
        <RowDefinition Height="50*"/>
        <RowDefinition Height="50*"/>
        <RowDefinition Height="50*"/>
    </Grid.RowDefinitions>
    <Button Grid.Row="0"></Button>
    <Button Grid.Row="1"></Button>
    <Button Grid.Row="2"></Button>
    <Button Grid.Row="3"></Button>
</Grid>

In that case all the Buttons would grow/shrink with the window. 在这种情况下,所有按钮都会随窗口一起增长/缩小。

But now i want to have it more dynamic. 但是现在我想让它更具动态性。 I have an ObservableCollection which contains all the elements to be added (dynamic amount). 我有一个ObservableCollection,其中包含所有要添加的元素(动态量)。 For the first implementation ive added all the elements to a StackPanel. 对于第一个实现,我把所有元素都添加到了StackPanel中。 But the controls within the StackPanel dosent resize so that i think about using a grid instead. 但是StackPanel主体中的控件会调整大小,因此我考虑改为使用网格。

Actual solution: 实际解决方案:

<Window.Resources>
    <DataTemplate DataType="{x:Type local:OwnObject}">
        <Button DataContext="{Binding}" Content="{Binding Text}" Margin="0,0,0,0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
    </DataTemplate>
</Window.Resources>
<Grid>
    <ItemsControl ItemsSource="{Binding SubItems}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel x:Name="stackPanel" Margin="0,0,0,0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
</Grid>

How is it possible to use the ItemsControl to generate one row for each element and add it to that row? 如何使用ItemsControl为每个元素生成一行并将其添加到该行? Other solutions to deal tith the problem are also welcome. 也欢迎其他解决该问题的解决方案。

You can use UniformGrid as ItemsPanelTemplate cause it's a grid where all the cells in the grid have the same size. 您可以使用UniformGrid作为ItemsPanelTemplate因为它是一个网格,在网格中的所有单元格的大小相同。 So the code will look like that. 因此代码看起来像这样。

<ItemsControl Name="icTest" VerticalContentAlignment="Stretch">
    <ItemsControl.ItemTemplate>
        <DataTemplate DataType="{x:Type local:OwnObject}">
            <DockPanel Margin="0">
                <Button Content="{Binding Text}" Margin="0,0,0,0"
                        HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
            </DockPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Columns="1" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

In code behind it's looks like that. 在后面的代码中看起来像那样。

    public class OwnObject : INotifyPropertyChanged
    {
        private string _text;

        public string Text
        {
            get { return _text; }
            set { _text = value; NotifyPropertyChanged( "Text" ); }
        }

...

    }

...

    ObservableCollection<OwnObject> objects = new ObservableCollection<OwnObject>();
    objects.Add( new OwnObject() { Text = "first" } );
    objects.Add( new OwnObject() { Text = "second" } );
    objects.Add( new OwnObject() { Text = "third" } );
    icTest.ItemsSource = objects;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM