2016-03-10 18 views
0

我看過很多類似的問題,但我認爲我可能會有足夠的不同以保證發佈它。CheckBox DelegateCommand沒有解僱

我簡單地將示例Azure移動服務應用程序遷移到Windows 10 UWP並嘗試保留所有MVVM。我有一個ListView具有DataTemplate,看起來像這樣:

<ListView.ItemTemplate> 
    <DataTemplate> 
     <StackPanel Orientation="Horizontal"> 
      <CheckBox Name="CheckBoxComplete" 
         Command="{Binding ElementName=ViewModel, Path=UpdateCommand}" 
         CommandParameter="{Binding}" 
         Margin="10,5" 
         VerticalAlignment="Center" 
         Content="{Binding Text}" 
         IsChecked="{Binding Complete, 
              Mode=TwoWay}" /> 
     </StackPanel> 
    </DataTemplate> 
</ListView.ItemTemplate> 

一切編譯罰款,但UpdateCommand不點火。我設置了一個斷點以確保正確的值將作爲CommandParameter傳遞,但Command未觸發。是否可以在DataTemplate以內使用DelegateCommand

或者我將不得不使用附加的行爲來實現這樣的事情?

編輯

原來CheckBox看起來像這樣,在代碼隱藏的單擊事件處理程序:

<CheckBox Name="CheckBoxComplete" 
      IsChecked="{Binding Complete, Mode=TwoWay}" 
      Checked="CheckBoxComplete_Checked" 
      Content="{Binding Text}" 
      Margin="10,5" VerticalAlignment="Center" /> 

EDIT2

按要求發佈整個XAML:

<Page x:Class="MyMoney10.Views.MainPage" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:behaviors="using:Template10.Behaviors" 
     xmlns:common="using:MyMoney10.Common" 
     xmlns:controls="using:Template10.Controls" 
     xmlns:core="using:Microsoft.Xaml.Interactions.Core" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:interactivity="using:Microsoft.Xaml.Interactivity" 
     xmlns:local="using:MyMoney10.Views" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:models="using:MyMoney10.Models" 
     xmlns:vm="using:MyMoney10.ViewModels" 
     mc:Ignorable="d"> 

    <Page.DataContext> 
     <vm:MainPageViewModel x:Name="ViewModel" /> 
    </Page.DataContext> 

    <RelativePanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
     <RelativePanel.ChildrenTransitions> 
      <TransitionCollection> 
       <EntranceThemeTransition /> 
      </TransitionCollection> 
     </RelativePanel.ChildrenTransitions> 

     <VisualStateManager.VisualStateGroups> 
      <VisualStateGroup x:Name="AdaptiveVisualStateGroup"> 
       <VisualState x:Name="VisualStateNarrow"> 
        <VisualState.StateTriggers> 
         <AdaptiveTrigger MinWindowWidth="{StaticResource NarrowMinWidth}" /> 
        </VisualState.StateTriggers> 
        <VisualState.Setters> 
         <!-- TODO: change properties for narrow view --> 
         <!--<Setter Target="StateTextBox.Text" Value="Narrow Visual State" />--> 
        </VisualState.Setters> 
       </VisualState> 
       <VisualState x:Name="VisualStateNormal"> 
        <VisualState.StateTriggers> 
         <AdaptiveTrigger MinWindowWidth="{StaticResource NormalMinWidth}" /> 
        </VisualState.StateTriggers> 
        <VisualState.Setters> 
         <!-- TODO: change properties for normal view --> 
         <!--<Setter Target="StateTextBox.Text" Value="Normal Visual State" />--> 
        </VisualState.Setters> 
       </VisualState> 
       <VisualState x:Name="VisualStateWide"> 
        <VisualState.StateTriggers> 
         <AdaptiveTrigger MinWindowWidth="{StaticResource WideMinWidth}" /> 
        </VisualState.StateTriggers> 
        <VisualState.Setters> 
         <!-- TODO: change properties for wide view --> 
         <!--<Setter Target="StateTextBox.Text" Value="Wide Visual State" />--> 
        </VisualState.Setters> 
       </VisualState> 
      </VisualStateGroup> 
     </VisualStateManager.VisualStateGroups> 

     <controls:PageHeader x:Name="PageHeader" Text="MyMoney"> 
      <!-- place stretched, across top --> 
      <RelativePanel.AlignTopWithPanel>True</RelativePanel.AlignTopWithPanel> 
      <RelativePanel.AlignRightWithPanel>True</RelativePanel.AlignRightWithPanel> 
      <RelativePanel.AlignLeftWithPanel>True</RelativePanel.AlignLeftWithPanel> 
      <!-- secondary commands --> 
      <controls:PageHeader.SecondaryCommands> 
       <AppBarButton Click="{x:Bind ViewModel.GotoSettings}" Label="Settings" /> 
       <AppBarButton Click="{x:Bind ViewModel.GotoPrivacy}" Label="Privacy" /> 
       <AppBarButton Click="{x:Bind ViewModel.GotoAbout}" Label="About" /> 
      </controls:PageHeader.SecondaryCommands> 
     </controls:PageHeader> 

     <controls:Resizer x:Name="ParameterResizer" Margin="16,16,16,0"> 

      <!-- place below page header --> 

      <RelativePanel.Below>PageHeader</RelativePanel.Below> 
      <RelativePanel.AlignLeftWithPanel>True</RelativePanel.AlignLeftWithPanel> 
      <TextBox MinWidth="250" 
        MinHeight="62" 
        Header="Parameter to pass" 
        Text="{Binding Value, 
            Mode=TwoWay, 
            UpdateSourceTrigger=PropertyChanged}"> 
       <interactivity:Interaction.Behaviors> 

        <!-- enable submit on enter key --> 

        <behaviors:KeyBehavior Key="Enter"> 
         <core:CallMethodAction MethodName="GotoDetailsPage" TargetObject="{Binding}" /> 
        </behaviors:KeyBehavior> 

        <!-- focus on textbox when page loads --> 

        <core:EventTriggerBehavior> 
         <behaviors:FocusAction /> 
        </core:EventTriggerBehavior> 
       </interactivity:Interaction.Behaviors> 
      </TextBox> 
     </controls:Resizer> 

     <Button x:Name="SubmitButton" 
       Click="{x:Bind ViewModel.GotoDetailsPage}" 
       Content="Submit"> 

      <!-- place next to textbox --> 

      <RelativePanel.RightOf>ParameterResizer</RelativePanel.RightOf> 
      <RelativePanel.AlignBottomWith>ParameterResizer</RelativePanel.AlignBottomWith> 
     </Button> 

     <TextBlock x:Name="StateTextBox" 
        Margin="16,16,0,0" 
        Text="Current Visual State"> 

      <!-- place under to textbox --> 

      <RelativePanel.Below>ParameterResizer</RelativePanel.Below> 
      <RelativePanel.AlignLeftWith>ParameterResizer</RelativePanel.AlignLeftWith> 
     </TextBlock> 

     <Grid> 
      <RelativePanel.Below>StateTextBox</RelativePanel.Below> 
      <RelativePanel.AlignLeftWithPanel>True</RelativePanel.AlignLeftWithPanel> 
      <RelativePanel.AlignRightWithPanel>True</RelativePanel.AlignRightWithPanel> 
      <RelativePanel.AlignBottomWithPanel>True</RelativePanel.AlignBottomWithPanel> 
      <Grid Margin="50,50,10,10"> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="*" /> 
        <ColumnDefinition Width="*" /> 
       </Grid.ColumnDefinitions> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="Auto" /> 
        <RowDefinition Height="*" /> 
       </Grid.RowDefinitions> 

       <Grid Grid.Row="0" 
         Grid.ColumnSpan="2" 
         Margin="0,0,0,20"> 
        <StackPanel> 
         <TextBlock Margin="0,0,0,6" 
            FontFamily="Segoe UI Light" 
            Foreground="#0094ff"> 
          MICROSOFT AZURE MOBILE SERVICES 
         </TextBlock> 
         <TextBlock FontFamily="Segoe UI Light" 
            FontSize="45" 
            Foreground="Gray"> 
          MyMoney10 
         </TextBlock> 
        </StackPanel> 
       </Grid> 

       <Grid Grid.Row="1"> 
        <StackPanel> 
         <common:QuickStartTask Title="Insert a TodoItem" 
               Description="Enter some text below and click Save to insert a new todo item into your database" 
               Number="1" /> 

         <StackPanel Margin="72,0,0,0" Orientation="Horizontal"> 
          <TextBox Name="TextInput" 
            MinWidth="300" 
            Margin="5" /> 
          <Button Name="ButtonSave" 
            Command="{x:Bind ViewModel.SaveCommand}" 
            CommandParameter="{Binding ElementName=TextInput, 
                   Path=Text}" 
            IsEnabled="{x:Bind ViewModel.SaveEnabled}"> 
           Save 
          </Button> 
         </StackPanel> 

        </StackPanel> 
       </Grid> 

       <Grid Grid.Row="1" Grid.Column="1"> 
        <Grid.RowDefinitions> 
         <RowDefinition Height="Auto" /> 
         <RowDefinition /> 
        </Grid.RowDefinitions> 
        <StackPanel> 
         <common:QuickStartTask Title="Query and Update Data" 
               Description="Click refresh below to load the unfinished TodoItems from your database. Use the checkbox to complete and update your TodoItems" 
               Number="2" /> 
         <Button Name="ButtonRefresh" 
           Margin="72,0,0,0" 
           Click="{x:Bind ViewModel.RefreshCommand}" 
           IsEnabled="{x:Bind ViewModel.RefreshEnabled}"> 
          Refresh 
         </Button> 
        </StackPanel> 

        <ListView Name="ListItems" 
           Grid.Row="1" 
           Margin="62,10,0,0" 
           ItemsSource="{Binding TodoItems}"> 
         <ListView.ItemTemplate> 
          <DataTemplate> 
           <StackPanel Orientation="Horizontal"> 
            <CheckBox Name="CheckBoxComplete" 
               Margin="10,5" 
               VerticalAlignment="Center" 
               Command="{Binding ElementName=ViewModel, 
                   Path=UpdateCommand}" 
               CommandParameter="{Binding}" 
               Content="{Binding Text}" 
               IsChecked="{Binding Complete, 
                    Mode=TwoWay}" /> 
           </StackPanel> 
          </DataTemplate> 
         </ListView.ItemTemplate> 
        </ListView> 

       </Grid> 

      </Grid> 
     </Grid> 
    </RelativePanel> 
</Page> 
+0

Command =「{Binding ElementName = ViewModel,Path = UpdateCommand}」如果替換爲Command =「{Binding ElementName = ViewModel,Path = DataContext.UpdateCommand}」應該解決問題 –

+0

我得到在'App.ViewModels.MainPageViewModel'類型的數據上下文中警告'無法解析屬性'DataContext'..我試過了,但它仍然無效。無論如何,我認爲添加'DataContext'將會是多餘的,因爲當你綁定某些東西時已經暗示了這一點。 –

+0

你可以發佈整個xaml嗎? –

回答

1

所以複選框綁定到elementname =「viewmodel」是問題。因爲viewmodel不是元素的名稱。

因此,給你的網頁一個名字,並在綁定中輸入該名稱。使用path = Datacontext.UpdateCommand

+0

'ViewModel'是一個元素名稱。看看上面在哪裏用'x:Name =「ViewModel」設置'Page.DataContext'' –

+0

我按照你的建議嘗試了,而且確實有效。我仍然不明白爲什麼它不適用於'ElementName = ViewModel'和'Path = UpdateCommand' .. –

+1

Elementname是用於xaml元素,而在您的情況下viewmodel它不是xaml UI元素的名稱,所以它沒有工作。 –