4

I was trying to use IsSelected set directly to true (w/o binding) during debugging of an issue (at the end I am trying to use binding, but have found that even w/o binding this does not work).

The following code works fine in WPF (all items selected) but does not work on WinRT (no item selected after execution).

Is this a bug/feature?

The following XAML will compile in a WPF window and in a WinRT page..

    <ListView SelectionMode="Multiple" HorizontalAlignment="Stretch">
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="IsSelected" Value="True"/>
            </Style>
        </ListView.ItemContainerStyle>
        <TextBox Width="200"/>
        <TextBox Width="200"/>
        <TextBox Width="200"/>
        <TextBox Width="200"/>
        <TextBox Width="200"/>
    </ListView>
Shahar Prish
  • 4,838
  • 3
  • 26
  • 47

1 Answers1

0

You can solve this problem using predefined datatemplate for listviewItem.Hope this helps

<ListView SelectionMode="Multiple">
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <ListViewItem  IsSelected="True" >
                            <TextBox Height="30" Width="200" ></TextBox>
                        </ListViewItem>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListView.ItemContainerStyle>
    <TextBox />
    <TextBox/>
    <TextBox/>
    <TextBox/>
    <TextBox/>        
</ListView>
Heena
  • 8,450
  • 1
  • 22
  • 40
  • That worked. Thanks. Now to see if it will work with binding. :) – Shahar Prish Apr 07 '14 at 13:47
  • Hmm.. No it didn't... You added a TextBox inside the style - I may have a different set of controls there (or bound controls). It also doesn't work with binding, it seems like.. :( – Shahar Prish Apr 07 '14 at 13:54
  • its datatemplate only and I think it should be worked with data bindings but in your case maybe other control dependencies create problems for data binding.not sure.thanks. – Heena Apr 07 '14 at 15:58
  • 1
    This does not work on WinRT and I doubt it works on Silverlight or WPF. – Trisped May 15 '14 at 23:55
  • @Trisped ..the above question is not for wpf and silverlight..read "this ListView.ItemContainerStyle IsSelected property does not seem to affect selection on WinRT"...i know my answer is not working for binding..but it works for selection in winrt..it seems you are new to xaml thats why you are assuming wpf,silverlight and winrt same.thank you – Heena May 18 '14 at 06:47
  • 1
    @HeenaPatil If you read my comment you will see that I am saying that your solution does not work for WinRT. It adds a second `ListViewItem` which is selected as bound, but that selection cannot be changed and you lose the select-ability of the `ListViewItem` created by the `ListView`. In short, you lose the whole reason for using the control in the first place. – Trisped May 19 '14 at 03:12