1

I have a list view that is hooked to an Observable collection as it's ItemsSource. I keep getting collection must be empty being using ItemsSource. Is this because I'm specifying the columns I want in the XMAL?

If so how would I make sure the IsChecked value is a checkbox and the rest text?

XMAL

<ListView x:Name="lstNextGen" HorizontalAlignment="Stretch" Height="430" Margin="10,95,10,0" VerticalAlignment="Top" Width="793">
                <Style TargetType="ListViewItem">
                    <Style.Triggers>
                        <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                            <Setter Property="Background" Value="WhiteSmoke" />
                        </Trigger>
                        <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                            <Setter Property="Background" Value="LightBlue" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
                <ListView.View>
                    <GridView>
                        <GridView.Columns>
                            <GridViewColumn>
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <CheckBox Tag="{Binding IsChecked}" IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}, Path=IsSelected}" />
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
                            <GridViewColumn DisplayMemberBinding="{Binding PatientName}" Header="Patient Name" Width="300px" />
                            <GridViewColumn DisplayMemberBinding="{Binding DOB}" Header="D.O.B." Width="75px" />
                            <GridViewColumn DisplayMemberBinding="{Binding AppointmentDate}" Header="Appointment Date" Width="150" />
                            <GridViewColumn DisplayMemberBinding="{Binding DocumentType}" Header="Document Type" Width="150px" />
                        </GridView.Columns>
                    </GridView>
                </ListView.View>
            </ListView>

C#

1 Answer 1

2

You should specify the style for the child items using the ItemContainerStyle otherwise it will be considered as child item and will be added to Items collection.

<ListView x:Name="lstNextGen" HorizontalAlignment="Stretch" Height="430" Margin="10,95,10,0" VerticalAlignment="Top" Width="793">
          <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Style.Triggers>
                    <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                        <Setter Property="Background" Value="WhiteSmoke" />
                    </Trigger>
                    <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                        <Setter Property="Background" Value="LightBlue" />
                    </Trigger>
                </Style.Triggers>
            </Style>
            </ListView.ItemContainerStyle>
            <ListView.View>
                <GridView>
                    <GridView.Columns>
                        <GridViewColumn>
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <CheckBox Tag="{Binding IsChecked}" IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}, Path=IsSelected}" />
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn DisplayMemberBinding="{Binding PatientName}" Header="Patient Name" Width="300px" />
                        <GridViewColumn DisplayMemberBinding="{Binding DOB}" Header="D.O.B." Width="75px" />
                        <GridViewColumn DisplayMemberBinding="{Binding AppointmentDate}" Header="Appointment Date" Width="150" />
                        <GridViewColumn DisplayMemberBinding="{Binding DocumentType}" Header="Document Type" Width="150px" />
                    </GridView.Columns>
                </GridView>
            </ListView.View>
        </ListView>
Sign up to request clarification or add additional context in comments.

1 Comment

I used this exact same implementation to achieve Style Triggers for my Checkbox controls. Works great!

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.