WPF listbox : problem with selection

本小妞迷上赌 提交于 2019-12-06 03:59:17

The problem is that the listbox cannout distinguish between the different values. Therefore, once you click one of the "10"s, it sets it SelectedItem property and updates its presentation. Because it cannot distinguish between the value types it marks every "10" as selected.

But why do you have "10" several times in your listbox? If it is just the numeric value 10 or the string "10" it does not make any sense to me.

If you have a more complex model behind that and you just display one property, than you should bind the complex model and set the DisplayMemberPath instead.

C#

public class Model
{
    public Guid Id { get; set; }
    public string Value { get; set; }
}

XAML

<ListBox ItemsSource="{Binding Path=Models}" DisplayMemberPath="Value" />

<ListBox ItemsSource="{Binding Path=Models}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=Value}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Best Regards
Oliver Hanappi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!