0

I need to display two columns of dataset; first column is the Checkbox items and second column would be just a list<string> I am able to build first column (grid.column=0) using Listbox with Checkbox in XAML as:

 <Grid Grid.Row="0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <ListBox x:Name="Listitems"  Grid.Column="0" SelectionMode="Multiple" ItemsSource="{Binding MonthlyResults}" >
                                <ListBox.ItemTemplate>
                                    <DataTemplate>
                                        <CheckBox  Content="{Binding logdate}" IsChecked="{Binding Checked ,Mode=TwoWay}"
                                             Click="CheckBox_Click"/>
                                    </DataTemplate>
                                </ListBox.ItemTemplate>
                            </ListBox>

                        </Grid>

User interface is displayed correctly as:

enter image description here

For the second column, I thought of using a ListView(grid.colum=1) and updated the above XAML as:

 <Grid Grid.Row="0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <ListBox x:Name="Listitems"  Grid.Column="0" SelectionMode="Multiple" ItemsSource="{Binding MonthlyResults}" >
                                <ListBox.ItemTemplate>
                                    <DataTemplate>
                                        <CheckBox  Content="{Binding logdate}" IsChecked="{Binding Checked ,Mode=TwoWay}"
                                             Click="CheckBox_Click"/>
                                    </DataTemplate>
                                </ListBox.ItemTemplate>
                             </ListBox>
                            <ListView Grid.Column="1" Name="CatNames" FontSize="13" SelectionMode="Extended" 
                                 ItemsSource="{Binding Path=OFMCategoriesNames}" IsSynchronizedWithCurrentItem="False" >
                        </ListView>
                        </Grid>

But the user interface is messed up. Both columns have their own scrollbars. enter image description here

How to display the columns in the same listbox ?

*************Update based on the answer provided*************** I updated the XAML as:

 <Grid Grid.Row="0">
                               <ListBox x:Name="Listitems"  SelectionMode="Multiple" ItemsSource="{Binding MonthlyResults}" >
                                <ListBox.ItemTemplate>
                                    <DataTemplate>
                                        <Grid>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="*" SharedSizeGroup="A1"/>
                                                <ColumnDefinition Width="*" SharedSizeGroup="A2"/>
                                            </Grid.ColumnDefinitions>
                                            <CheckBox Grid.Column="0" Content="{Binding logdate}" IsChecked="{Binding Checked ,Mode=TwoWay}" Click="CheckBox_Click"/>
                                            <ListView Grid.Column="1" Name="CatNames" FontSize="13" SelectionMode="Extended" 
                                             ItemsSource="{Binding MeasureMethod}" IsSynchronizedWithCurrentItem="False" >
                                            </ListView>
                                        </Grid>

                                    </DataTemplate>
                               </ListBox.ItemTemplate>

                             </ListBox>

                        </Grid>

Even after adding "sharedsizegroup", the columns are not aligned properly..

enter image description here

1 Answer 1

2

You can either use a Grid view inside the ListView like

<ListView.View>
    <GridView>
        ....
    </GridView>
</ListView.View>

or you can use a grid inside your template

<ListBox.ItemTemplate>
     <DataTemplate>
          <Grid Grid.IsSharedSizeScope="True">
              <Grid.ColumnDefinitions>
                   <ColumnDefinition SharedSizeGroup="A"/>
                   <ColumnDefinition SharedSizeGroup="B"/>
                   <ColumnDefinition SharedSizeGroup="C"/>
              </Grid.ColumnDefinitions>
              <CheckBox Grid.Column="0"/>
              <TextBlock Grid.Column="1"/>
              <TextBlock Grid.Column="2"/>
          </Grid>
      </DataTemplate>
</ListBox.ItemTemplate>
Sign up to request clarification or add additional context in comments.

5 Comments

The columns in the ListBox Grid can be made to align using <ListBox Grid.IsSharedSizeScope="True" and SharedSizeGroup="ColumnA" etc on the ColumDefinitions
@EdPlunkett : thnks but even after adding the SharedSizegroup, the columns are no aligned..
@Steve: thanks I updated the question based on your suggestion but columns are not aligned.
@user7157732 It works for me every single time. Let's see the XAML.
@user7157732 <ListBox Grid.IsSharedSizeScope="True". Add Grid.IsSharedSizeScope="True" to the ListBox. You can use SharedSizeGroup all over the place in a XAML file -- there has to be some way to define the scope in which the size is shared. The way you do that is you set Grid.IsSharedSizeScope="True" on some containing element. ListBox is a good choice for the containing element in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.