0

MVVM ライト パターンを使用して Windows Phone アプリを作成しています。選択したインデックスに対して常に負の値 (-1) を返すため、リスト ボックスで問題が発生しています。誰もそれを解決する方法を知っていますか?

これがView Modelの私のコードです。何か見逃していませんか?ありがとう!

 public void OnViewListSelectedItem(SelectionChangedEventArgs e)
    {

        ListBox lb = new ListBox();

        if (e.AddedItems.Count == 1)
        {
            if (lb.SelectedIndex == 0)
            {
                _navigationService.NavigateTo(new Uri(ViewModelLocator.ByVendorUrl, UriKind.Relative));
            }

            if (lb.SelectedIndex == 1)
                {
                    _navigationService.NavigateTo(new Uri(ViewModelLocator.ByVendorUrl, UriKind.Relative));
                }
            if (lb.SelectedIndex == 2)
                {
                    _navigationService.NavigateTo(new Uri(ViewModelLocator.ByCombinationUrl, UriKind.Relative));
                }
            }
    }

XAML コードはこちら

 <ListBox x:Name="lbviewlist">
                <i:Interaction.Triggers>
                        <i:EventTrigger EventName="SelectionChanged">
                            <Command:EventToCommand Command="{Binding ViewListCommand}" PassEventArgsToCommand="True"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                    <ListBox.Items>
                    <ListBoxItem Content="By Product" FontSize="35" Margin="10,12,12,0"/>
                    <ListBoxItem Content="By Vendor" FontSize="35" Margin="10,12,12,0"/>
                    <ListBoxItem Content="By Best Combination" FontSize="35" Margin="10,12,12,0"/>
                    </ListBox.Items>
                </ListBox>
4

2 に答える 2

2

コードで新しい ListBox() (lb と呼ばれる) を作成しています。データを入力しないため、空になり、常に SelectedIndex が -1 になります。

次に、「e」の「Source」プロパティを確認し、ListBox にキャストします。

ListBox myList = (ListBox) e.Source;

その後、myList のプロパティにアクセスできます。

于 2013-04-19T03:01:47.443 に答える
1

私の調査によると、リストボックスの SelectedIndex プロパティはバインド可能ではありません。SelectedIndex プロパティに get アクセサーを使用すると、常に -1 が返されます。SelectedIndex プロパティに set アクセサーを使用しようとすると、NotSupportedException が発生します。-- MSDN リストの選択されたプロパティ

新しいリストボックスを作成し、その結果が空/nullになる最初のコードが間違っているため、コードも更新しました。また、selectionchanged イベントはイベントとして使用しても問題ありません。

    public void method (SelectionChangedEventArgs e)
    {


        {                
            if (e.AddedItems.Count == 1)
            {
                var listBoxItem = (ListBoxItem)e.AddedItems[0];
                string _string1 = "Test";
                if ((string)listBoxItem.Content == _string1)
                {
                    navigationService.NavigateTo(new Uri(ViewModelLocator.page1, UriKind.Relative));
                }
            }
         }
}

それでおしまい。それが役に立てば幸い!:)

于 2013-04-19T09:40:52.997 に答える