gpt4 book ai didi

c# - MVVM listview 绑定(bind)到字典 selecteditem 问题

转载 作者:行者123 更新时间:2023-11-30 14:34:56 37 4
gpt4 key购买 nike

我正在使用 MVVM,我有一个 ListView ,它绑定(bind)到我的 View 模型中的字典。在我的 xaml 中,我设置了 selecteditem 以设置正确的选定项目,但不幸的是我所做的不起作用。如果我将其更改为 selectedindex 并将属性数据类型更改为 int,我就可以设置它。这是我的观点。

查看(xaml)

<Grid Width="auto" Grid.Column="0" Grid.Row="1" HorizontalAlignment="Left" Margin="0,1,0,0">
<Grid.RowDefinitions>
<RowDefinition Height="auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="188"></ColumnDefinition>
</Grid.ColumnDefinitions>
<ListView Name="lstNewPatient" HorizontalAlignment="Left" VerticalAlignment="Bottom"
Height="auto" Margin="0,0,0,0" ItemsSource="{Binding Path=TestList}" SelectedItem="{Binding Path=SelectedTestItem}"
FontSize="11" SelectionMode="Single">
<ListView.View>
<GridView>
<GridView.ColumnHeaderContainerStyle>
<Style>
<Setter Property="FrameworkElement.Visibility" Value="Collapsed"/>
</Style>
</GridView.ColumnHeaderContainerStyle>
<GridViewColumn Width="69" DisplayMemberBinding="{Binding Key}"></GridViewColumn>
<GridViewColumn Width="109" DisplayMemberBinding="{Binding Value}"></GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>

View 模型

Dictionary<int, string> selectedTestItem= null;
public Dictionary<int, string> SelectedTestItem
{
get
{
return selectedTestItem;
}
set
{
selectedTestItem = value;
OnPropertyChanged("SelectedTestItem");
}
}

private Dictionary<int, string> TestList()
{
var newTestList = new Dictionary<int, string>();

newTestList.Add(1, "Apple");
newTestList.Add(2, "Car");
newTestList.Add(3, "Laptop");


return newPtLevelList;
}

最佳答案

SelectedTestItem应该是 KeyValuePair<int, string> , 不是 Dictionary<int, string>

关于c# - MVVM listview 绑定(bind)到字典 selecteditem 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13092441/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com