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

佐手、 提交于 2019-12-11 18:59:56

问题


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>


来源:https://stackoverflow.com/questions/16898272/why-i-cant-bind-the-command-to-the-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!