3

リストビューにコマンドを追加したいと思います。アイテムがクリックされたら、relayCommand を実行したいと思います。今、私はこれらの解決策を持っています、それは動作しますが、MVVMではないと思います:)

<ListView ItemsSource="{Binding Taxons}" IsItemClickEnabled="True" ItemClick="ListViewBase_OnItemClick">
       <ListView.ItemTemplate>
            <DataTemplate>
                  <TextBlock Text="{Binding Name}"/>
            </DataTemplate>
       </ListView.ItemTemplate>
</ListView>

コードビハインド:

private void ListViewBase_OnItemClick(object sender, ItemClickEventArgs e)
{
       viewModel.TreeItemSelected.Execute(((Taxon)e.ClickedItem).Name);
}

コードビハインドなしでこの方法を使用したいのですが、VSが私に言うようにそれは不可能です:

<ListView ItemsSource="{Binding Taxons}" IsItemClickEnabled="True" ItemClick="{Binding TreeItemSelected}">
       <ListView.ItemTemplate>
            <DataTemplate>
                  <TextBlock Text="{Binding Name}"/>
            </DataTemplate>
       </ListView.ItemTemplate>
</ListView>

次の追加の dll-s を使用しますが、可能であれば他のものをインストールしたくありません。GalaSoft.MvvmLight.Extras.Win8 GalaSoft.MvvmLight.Win8

私に解決策を提案できますか?

4

3 に答える 3

4

これが実際の例です:

<ListBox x:Name="name_here" 
      ItemsSource="{Binding your_item_source}"
      SelectedItem="{Binding your_selected_item, UpdateSourceTrigger=PropertyChanged}"
     ...
      >
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseDoubleClick">
            <Command:EventToCommand Command="{Binding your_command_here}" 
                                CommandParameter="{Binding ElementName=name_here, Path=SelectedItem}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ListBox>

Blend の Interaction トリガーを利用します (blend をインストールする必要はありません。それがなければ dll で十分です)。また、 を使用している場合は、 をコントロールの名前にCommandParameterバインドします (この例では) 。ElementNamename_here

于 2013-10-20T06:34:52.153 に答える
3

同じ問題を抱えている人のために、別の方法があると言いたいです。ここに示すように 、 ListViewアイテムに対して
を定義し、 ViewModelのコマンドにバインドできます。TapGestureRecognizer

<StackLayout x:Name="StackLayout1" Orientation="Vertical" HorizontalOptions="StartAndExpand" VerticalOptions="FillAndExpand">    
   <StackLayout.BindingContext>
       <viewModels:NotificationsViewModel />
   </StackLayout.BindingContext>

   <ListView ItemsSource="{Binding NotificationsList}">
     <ListView.ItemTemplate>
        <DataTemplate>
          <ViewCell>
            <StackLayout Orientation="Vertical">  

              <StackLayout.GestureRecognizers>  <!-- here you bind the TAP event to your ViewModel -->
                <TapGestureRecognizer Command="{Binding Path=BindingContext.OnListItemTapped, Source={x:Reference StackLayout1}}" 
                                      CommandParameter="{Binding}"/>
              </StackLayout.GestureRecognizers>

              <RelativeLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
                <Image x:Name="IsReadImage" StyleId="IsReadImage" WidthRequest="{StaticResource OptionImageSize}"
                       Source='{Binding IsRead, Converter={StaticResource BooleanToValueConverter}, ConverterParameter="Resources.emailOpenIcon,Resources.emailIcon"}'
                       RelativeLayout.YConstraint="{ConstraintExpression Type=Constant, Constant=0}"
                       RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.8, Constant=0}"></Image>

                <Label Text="{Binding Title}" x:Name="TitleLabel" HorizontalTextAlignment="Start" FontAttributes='{Binding IsRead, Converter={StaticResource BooleanToValueConverter}, ConverterParameter="Bold?!"}'
                       RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToView, ElementName=IsReadImage, Property=Width, Constant=5}"
                       RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToView, ElementName=IsReadImage, Property=Y, Constant=3}" /></RelativeLayout>
            </StackLayout>
          </ViewCell>
        </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>
</StackLayout>

そして、ページのViewModelでコマンドを定義できます

public ICommand OnListItemTapped
{
    get
    {
        return new Command<Notification>(item =>
        {
            Debug.WriteLine(item.Title + " Cliked!");
        });
    }
}
于 2016-04-13T20:01:02.900 に答える
0

この 2 つの方法を実行できます。1 つは、リスト ビューの SelectedItem をビューモデルのプロパティにバインドすることです。(そのプロパティを SelectedTaxon と呼ぶかもしれません)、プロパティ セッターでは、ビューモデルに対して任意のアクションを実行できます。

逆に、ビューモデルでコマンドを実行することに本当に縛られている場合。mvvmlight EventToCommand 動作を使用できます。それに関する情報は、Google で見つけることができます。または、このリンクでその使用方法の説明があります。http://adammills.wordpress.com/2011/02/14/eventtocommand-action-mvvm-glue/

あなたが持っているmvvmlight extras dllの一部として、あなたはすでにその動作をしていると思います。

于 2013-10-20T03:01:27.960 に答える