Why I can't bind the Command to the button?

2019-08-29 04:47发布

问题:

I work on a project target on Windows Phone 7.5 and above.

What I have
ListBox

<ListBox    HorizontalAlignment="Left" 
        VerticalAlignment="Top" 
        SelectedItem="{Binding singleFavListItem, Mode=TwoWay}"
        ItemTemplate="{StaticResource userFavBoardListItemTemplate}"
        ItemsSource="{Binding userfavboardlist}" 
        ScrollViewer.VerticalScrollBarVisibility="Disabled" Margin="12,0,0,12"/>

ItemTemplate

<DataTemplate x:Key="userFavBoardListItemTemplate">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="70*"/>
            <ColumnDefinition Width="30*"/>
        </Grid.ColumnDefinitions>
        <TextBlock  HorizontalAlignment="Left" 
            TextWrapping="Wrap" 
            Text="{Binding boardName}" 
            VerticalAlignment="Center" 
            FontSize="{StaticResource PhoneFontSizeMedium}" 
            Foreground="{StaticResource TitleColor}"/>
        <Button Command="{Binding quitBoardCommand}"
                CommandParameter="{Binding boardUrl}"
                Content="Quit" 
        HorizontalAlignment="Left" 
        VerticalAlignment="Top" 
        Grid.Column="1"
        FontSize="{StaticResource PhoneFontSizeSmall}" 
        BorderBrush="{StaticResource DateArticalCategoryColor}" 
        Foreground="{StaticResource DateArticalCategoryColor}">
     </Button>
    </Grid>
</DataTemplate>

ViewModel

public MyFavListViewModel()
{
    this._quitBoardCommand = new DelegateCommand(this.quitBoardAction);
}
private ICommand _quitBoardCommand;
public ICommand quitBoardCommand
{
    get
    {
        return this._quitBoardCommand;
    }
}
private void quitBoardAction(object p)
{
    //my business logic here
}

Error
I found a error in the OutPut windows:

'xicihutong.Model.UserFavBoardListRawData' (HashCode=55845053). BindingExpression: Path='quitBoardCommand' DataItem='xicihutong.Model.UserFavBoardListRawData' (HashCode=55845053); target element is 'System.Windows.Controls.Button' (Name=''); target property is 'Command' (type 'System.Windows.Input.ICommand')..

What's the problem
What confuse me is that the quitBoardCommand never get triggered when I tap the button? It seems that I can't bind the Command to the button, the DelegateCommand part is right, because I can use it to bind command in other pages. And the SelectedItem of the ListBox works right, also.

Why I can't bind this one?

回答1:

You need to reference the DataContext of your ListBox to bind to your command. To fix this, give your ListBox a name then reference the command property

<ListBox x:Name="myLB"
    <!-- rest of your stuff -->
/>

<Button 
    Command="{Binding Path=DataContext.quitBoardCommand, ElementName=myLB}"
    CommandParameter="{Binding boardUrl}"
    Content="Quit" />


回答2:

Can you try this code. You don't forget to change "datacontext" name.

<DataTemplate x:Key="userFavBoardListItemTemplate">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="70*"/>
            <ColumnDefinition Width="30*"/>
        </Grid.ColumnDefinitions>
        <TextBlock  HorizontalAlignment="Left" 
            TextWrapping="Wrap" 
            Text="{Binding boardName}" 
            VerticalAlignment="Center" 
            FontSize="{StaticResource PhoneFontSizeMedium}" 
            Foreground="{StaticResource TitleColor}"/>
        <Button Command="{Path=quitBoardCommand,Source={StaticResource datacontext}}"
                CommandParameter="{Binding boardUrl}"
                Content="Quit" 
        HorizontalAlignment="Left" 
        VerticalAlignment="Top" 
        Grid.Column="1"
        FontSize="{StaticResource PhoneFontSizeSmall}" 
        BorderBrush="{StaticResource DateArticalCategoryColor}" 
        Foreground="{StaticResource DateArticalCategoryColor}">
     </Button>
    </Grid>
</DataTemplate>