1

リスト ビューから selectedItem を取得しようとしています。ボタンでMVVMライトツールキットとEventToCommandを使用しています。

私のlistViewは、正しくバインドされているObservableCollectionにバインドされています。listView xaml は次のとおりです。

  <ListView   Name="serverListView"
                    Grid.Row="3"
                    Grid.Column="0"
                    Grid.ColumnSpan="2"
                    ItemsSource="{Binding Servers}"
                    ItemTemplate="{StaticResource ServerList}"
                    SelectionMode="Single"
                    BorderThickness="0"/>

次に、mvvm EventToCommand で Interaction.Triggers を使用しているボタンがあります。selectedItem バインディングが正しいかどうかはわかりません。イベントはリレー コマンド (mvvm light ツールキット) を介して正しく発生していますが、毎回 null になります。

これが私のボタンの xaml です。

<Button x:Name="LoadButton"
                Content="Load Server"
                Grid.Column="0"
                Grid.Row="4"
                Grid.ColumnSpan="2">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Click">
                    <mvvm:EventToCommand  Command="{Binding ButtonClick, Mode=OneWay}"
                                            CommandParameter="{Binding SelectedItem, ElementName=serverListView}"
                                            MustToggleIsEnabledValue="True"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Button>

中継コマンド:

 this.ButtonClick = new RelayCommand<object>(new Action<object>(this.GetClickEvent));
4

2 に答える 2

1

コマンドパラメータバインディングを取り除き、ビューモデルにSelectedServerプロパティを作成し、パラメータなしでビューモデルにコマンドを作成します

<ListView   Name="serverListView"
                Grid.Row="3"
                Grid.Column="0"
                Grid.ColumnSpan="2"
                ItemsSource="{Binding Servers}"
                ItemTemplate="{StaticResource ServerList}"
                SelectedItem="{Binding SelectedServer}"
                SelectionMode="Single"
                BorderThickness="0"/>

<Button Command="{Binding MyCommand}" />

ビューモデルには、コマンドロジックを実行するために必要なすべての情報があります。

于 2012-06-14T10:41:09.627 に答える
0

リストビューの SelectedItem プロパティをビューモデルのプロパティ (SelectedServer) にバインドし、EventToCommand を変更する必要があります。

CommandParameter="{Binding SelectedItem, ElementName=serverListView}"

CommandParameter="{Binding SelectedServer}"
于 2012-06-14T10:15:40.000 に答える