为什么我不能绑定到按钮的命令? [英] Why I can't bind the Command to the button?
问题描述
我在Windows Phone 7.5及以上版本的项目目标上工作。
我有
ListBox
< ListBox HorizontalAlignment =Left
VerticalAlignment =Top
SelectedItem ={Binding singleFavListItem ,Mode = TwoWay}
ItemTemplate ={StaticResource userFavBoardListItemTemplate}
ItemsSource ={Binding userfavboardlist}
ScrollViewer.VerticalScrollBarVisibility =DisabledMargin =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)
{
//我的业务逻辑
}
错误
我在OutPut窗口中发现错误:
< blockquote>
'xicihutong.Model.UserFavBoardListRawData'(HashCode = 55845053)。
BindingExpression:Path ='quitBoardCommand'
DataItem ='xicihutong.Model.UserFavBoardListRawData'
(HashCode = 55845053);目标元素是
'System.Windows.Controls.Button'(Name ='');目标属性是
'Command'(输入'System.Windows.Input.ICommand')..
有什么问题
什么让我困惑的是,当我点击按钮时,quitBoardCommand永远不会被触发?看来我不能将命令绑定到按钮,DelegateCommand部分是正确的,因为我可以使用它来绑定命令在其他页面。
为什么我不能绑定这个呢?
解决方案您需要引用
ListBox
的DataContext
绑定到您的命令。要解决这个问题,请给你的ListBox
一个名称,然后引用命令属性< ListBox x:Name =myLB
<! - 其余的东西 - >
/>
< Button
Command ={Binding Path = DataContext.quitBoardCommand,ElementName = myLB}
CommandParameter ={Binding boardUrl}
Content =退出/>
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?
解决方案You need to reference the
DataContext
of yourListBox
to bind to your command. To fix this, give yourListBox
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" />
这篇关于为什么我不能绑定到按钮的命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!