WPF - How to include specific ListView items in columns?

I have a dataset that I would like to present through a ListView WPF in this way:

Column1 Column2 Column3
--GroupName1--
Item1 part2 part3
Item2 part2 part3
--GroupName2--
Item3 part2 part3
Item4 long_text_in_both_columns
Item5 part2 part3
--GroupName1--
Item6 part2 part3
Item7 long_text_in_both_columns
--GroupName3--
Item8 part2 part3

I start by working with this basic sample: http://msdn.microsoft.com/en-us/library/ms771309(VS.90).aspx

Item4 and Item7 have long text that I would like to cover the remaining columns (ignoring what the original column headers were for). How can i do this?

I already have some XAML settings with a DataTrigger to replace the default GridViewRowPresenter with a custom TextBlock, but that's not quite what I'm looking for. I need the data in column 1 to display normally and the width of the recognized first column.

+4
source share
3 answers

Here is how I decided to solve this using the appropriate ListView:

        <ListView.ItemContainerStyle >
            <Style TargetType="ListViewItem">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ShowAcrossColumns}" Value="True">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type ListViewItem}">
                                    <Grid>
                                        <Grid>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="{Binding ElementName=myListView, Path=View.Columns[0].Width}" />
                                                <ColumnDefinition Width="*" />
                                            </Grid.ColumnDefinitions>
                                            <TextBlock Grid.Column="0" Padding="6,3,6,3" Text="{Binding Column1Text}" />
                                            <TextBlock Grid.Column="1" Padding="6,3,6,3" Text="{Binding ColumnSpanningText}" />
                                        </Grid>
                                    </Grid>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                </Style.Triggers>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListViewItem}">
                            <Grid>
                                <GridViewRowPresenter />
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListView.ItemContainerStyle>

, DataTriggered GridViewRowPresenter . / , , GridViewRowPresenter . Grid ListView. , .

+7

, ListBox ListView ItemTemplate .

<ListBox>
<ListBox.ItemTemplate>
 <DataTemplate>
  <StackPanel>
    <TextBlock Text="{Binding Text1}"/>
    <TextBlock Text="{Binding Text2}" />
  </StackPanel>
 </DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

, Trigger , .

:)

0

, , , 5- ( ) . @PeteVasi :

  • ActualWidth Width, , , .
  • RelativeSource myListView ( ).

Width ColumnDefinition :

Width="{Binding ElementName=myListView, Path=View.Columns[0].Width}"

:

Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
    AncestorType={x:Type ListView}}, Path=View.Columns[0].ActualWidth}"

, <Grid>, , .

I experimented with a small answer option that uses GridViewRowPresenterand played with a bad idea (by tracking column resizing events and using them to set a property to which the column width is bound). Various experiments, including a fully stylized version, can be found in the DisasmUiTest project on github .

0
source

Source: https://habr.com/ru/post/1770389/


All Articles