UWP Listview with itemswrapgrid setting width based on items content

廉价感情. 提交于 2019-12-24 09:28:25

问题


I have an Listview with togglebuttons in datatemplate in UWP. I have configured the listview as itemwrapgrid. I am trying to assign diffrent width for items based on toggle buttons content length. Can anyone help me to do this.

 <ListView x:Name="lstVw1" IsMultiSelectCheckBoxEnabled="True" Margin="0,20,0,0">
                    <ListView.ItemsPanel>
                        <ItemsPanelTemplate>
                            <ItemsWrapGrid Orientation="Horizontal" Width="Auto" HorizontalAlignment="Stretch"></ItemsWrapGrid>
                        </ItemsPanelTemplate>
                    </ListView.ItemsPanel>
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ToggleButton Style="{StaticResource ButtonStyle}" Content="{Binding ItemText}" Click="ToggleButton_Click">
                            </ToggleButton>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                    <ListView.ItemContainerStyle>
                        <Style TargetType="ListViewItem">
                            <Setter Property="HorizontalContentAlignment" Value="Stretch" />

                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate>
                                        <ContentPresenter Padding="5"/>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>

                    </ListView.ItemContainerStyle>
                </ListView>

The above xaml gives equaly sized toggle buttons in the list


回答1:


I would use the WrapPanel from the UWP Community Toolkit. You can download the nuget package and then change your ItemsPanel of your ListView to be a WrapPanel

<ListView.ItemsPanel>
    <ItemsPanelTemplate>
        <wrapPanel:WrapPanel Orientation="Horizontal"/>
    </ItemsPanelTemplate>
</ListView.ItemsPanel>

Note that the WrapPanel was accidentally put into the Microsoft.Toolkit.Uwp.Controls.WrapPanel namespace, so you'll need to declare the wrapPanel namespace in your XAML

xmlns:wrapPanel="using:Microsoft.Toolkit.Uwp.Controls.WrapPanel"



回答2:


I have created a custom wrap panel for the above.

Reference




回答3:


It works exactly like that by default - ItemTemplate will expand to fit the content. You might need to use ItemsStackPanel as ItemsPanelTemplate.

What you did wrong is this line, you should remove it:

<Setter Property="HorizontalContentAlignment" Value="Stretch" />


来源:https://stackoverflow.com/questions/43975108/uwp-listview-with-itemswrapgrid-setting-width-based-on-items-content

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!