soma sekhar
soma sekhar

Reputation: 259

unable to select listbox item through data template in WPF

I am unable to select desired item from the listbox (when i click on any item in that listbox the items more than one are getting selected but not the one on which i have clicked). Also the background color of the selected items are getting changed to default (white) color. The Xaml code used by me is as follows:

<ListBox x:Uid="lst_value" Name="lstValues" Background="Wheat"
    BorderBrush="Black" HorizontalAlignment="Left" VerticalAlignment="Top"
    BorderThickness="1" Height="100" Width="150" ItemsSource="{Binding listval}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical" Background="Wheat">
                <TextBlock x:Name="txtblk" Foreground="Black" FontSize="10"
                    TextAlignment="Left" FontWeight="Black" Text="{Binding}"
                    Background="Wheat"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Could anyone let me know how to resolve the issue please.

Upvotes: 0

Views: 2120

Answers (4)

brentlightsey
brentlightsey

Reputation: 2066

In my case, I was wrongly adding ComboBoxItems to the ListBox.Items collection. Adding objects other than a "ListBoxItem" to the Items collection caused this behavior for me.

Upvotes: 0

brunnerh
brunnerh

Reputation: 185509

You should not set the Background on the StackPanel and TextBlock, that obfuscates the selection. To override the background for the selection add resources to your ListBoxItems.

<ListBox.ItemContainerStyle>
    <Style TargetType="ListBoxItem">
        <Style.Resources>
            <!-- Selected Brush -->
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Green" />
            <!-- Selected but out of focus Brush -->
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="LightGreen" />
        </Style.Resources>
        <!-- If you must set a Background, do it here, should be superfluous though as the ListBox.Background is the same -->
        <Setter Property="Background" Value="Wheat" />
    </Style>
</ListBox.ItemContainerStyle>

The selection issue can appear if you have a source collection with identical objects (like strings that have the same value).

Upvotes: 3

HCL
HCL

Reputation: 36805

If I understand your problem right, then the selection logic of the listbox reacts weird. Right?

In most cases, such behaviour has to do with the Equals() or/and the GetHashCode() method of your items (the objects in your listVal enumeration). Make sure you have not multiple objects in your list that return true for a call to Equals() of one object. Make also sure, you have not objects that return changing values for GetHashCode() (some random values).

If you found the problem in the above methods (I assume Equals) but you must say that you can not change the implementation of Equals(), consider creating a wrapper object for your items (a ViewModel).

Upvotes: 1

Bathineni
Bathineni

Reputation: 3496

If i am not wrong listval is a List<string> or any other list of primitive types.. Reason why it is behaving wired with your case is primitive types are struct not classes.

instead of using list of primitive types try using concrete class's

Lets say it you are using List of names like List..

Create a class having name property

class person
{
public string Name{get;set;} 
}

use List and you can bind the name property

Upvotes: 0

Related Questions