2017-07-10 7 views
2

MVVM Crossを使用してUWPアプリケーションを構築しています。ボタンにコマンドをバインドする際に問題があります。以前のXAMLページでは、他のコマンドをバインドするための対話機能を使用しました。ここでの問題は、リストビューを使用してユーザーの「お気に入りリスト」を表示していることです。 UnfavoriteCommandはViewModelにありますが、User.Favoriteリストがモデルにあり、 "UnfavoriteCommand"を持たないため、ヒットすることはありません。 MVVM Crossを使用してこのバインディングの問題を処理するにはどうすればよいですか?listviewの中からページのdatacontextにアクセスするには?

  <ListView 
       Grid.Row="0" 
       Grid.Column="0" 
       CanReorderItems="True" 
       CanDrag="True" 
       AllowDrop="True" 
       ItemsSource="{Binding User.FavoriteList}"> 
        <ListView.ItemTemplate> 
         <DataTemplate> 
          <Grid> 
           <Grid.RowDefinitions> 
            <RowDefinition Height="*" /> 
           </Grid.RowDefinitions> 
           <Grid.ColumnDefinitions> 
            <ColumnDefinition Width="500" /> 
            <ColumnDefinition Width="*" /> 
           </Grid.ColumnDefinitions> 
           <StackPanel Grid.Column="0" Grid.Row="0" 
             HorizontalAlignment="Left" 
             Margin="0, 0, 0, 0" 
             Height="25" > 
            <TextBlock Text="{Binding Description, Mode=TwoWay}" 
                VerticalAlignment="Center" 
                TextAlignment="Left"/> 
           </StackPanel> 
           <StackPanel VerticalAlignment="Center" Grid.Column="1" Grid.Row="0"> 
            <Button Content="&#xf00d;" 
              FontFamily="{StaticResource FontAwesomeFontFamily}"             
              BorderBrush="Black"> 

             <Interactivity:Interaction.Behaviors> 
              <Core:EventTriggerBehavior EventName="Click"> 
               <Core:EventTriggerBehavior.Actions> 
                <Core:InvokeCommandAction CommandParameter="{Binding}" Command="{Binding UnfavoriteCommand}"/> 
                <!--<Core:CallMethodAction TargetObject="{Binding}" MethodName="Unfavorite" />--> 
                <!--<Core:InvokeCommandAction Command="{Binding UnfavoriteCommand}" InputConverter="{StaticResource buttonConverter}"/>--> 
               </Core:EventTriggerBehavior.Actions> 
              </Core:EventTriggerBehavior> 
             </Interactivity:Interaction.Behaviors> 
            </Button> 
           </StackPanel> 
          </Grid> 
         </DataTemplate> 
        </ListView.ItemTemplate> 
       </ListView> 

答えて

2

ページに名前を付け、ElementNameバインディングを使用します。

<Core:InvokeCommandAction CommandParameter="{Binding}" Command="{Binding DataContext.UnfavoriteCommand, ElementName=PageName}"/> 
+0

これはうまくいった!ありがとう、ジェシカ。 –

0

このコードは、WPFのapplcation用ですが、私はそれはあなたがのようにViewModelに(インターフェイスのみ)持ってしてもよいし、別々のソースまたはコンテキストを参照するためにRelativeSourceを使用する必要があり、UWPアプリケーションと同じだと思う:

public class IAnyViewModel { 
    RelayCommand UnfavoriteCommand {get;} 
    ObservableCollection<UserFavorite> FavoriteList {get;set;} 
} 

そして、このクラスは、このようないくつかの事コンストラクタまたはコードによって、ページやウィンドウcontrollに割り当てられている:

public constructorControl(){ 
    this.DataContext = new AnyViewModel(); 
} 

またはXAMLによります。

<Page.DataContext> 
<local:AnyViewModel></local:AnyViewModel> 
</Page.DataContext> 

次に、あなたは、このようにいくつかのRelativeSourceを使用して、親コンテキストに参照することができます

<Core:InvokeCommandAction 
    Command="{Binding UnfavoriteCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}}"> 

私はあなたのコントロールがページであると仮定しています。

+0

uwpでFindAncestorバインディングがありません。 –

+0

はい、そうです、あなたは "RelativeSource FindAncestor in UWP"を検索することができます。Jessicaはもっと近くにあります。(例:https://stackoverflow.com/questions/32861612/how-to-do-relativesource-mode- find-ancestor-or-equivalent-uwp) –

関連する問題