3

私はMVVMを使用しており、ビューモデルの辞書にバインドされたリストビューがあります。私のxamlでは、selecteditemを設定して正しい選択項目を設定しましたが、残念ながら私がやっていることはうまくいきません。プロパティのデータ型を int に変更して selectedindex に変更すると、設定できます。これが私の見解です。

表示(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>

ビューモデル

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 に答える