3

我正在使用 MVVM,并且我有一个列表视图,它绑定到我的视图模型中的字典。在我的 xaml 中,我设置了 selecteditem 以设置正确的选定项目,但不幸的是我正在做的事情不起作用。如果我将其更改为 selectedindex 并将属性数据类型更改为 int,我可以设置它。这是我的看法。

查看(xml)

<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>

视图模型

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;
}
4

1 回答 1

6

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

于 2012-10-26T18:34:44.740 回答