C# 如何从MVVM绑定调用命令
我有以下代码使用MVVM绑定C# 如何从MVVM绑定调用命令,c#,windows-phone-8,C#,Windows Phone 8,我有以下代码使用MVVM绑定ListBox数据。我想从MVVM执行命令,数据被完全绑定,我不知道为什么它不能与命令一起工作。单击按钮时,我没有收到消息 视图模型 public class BookmarkViewModel : INotifyPropertyChanged { public BookmarkViewModel() { DataSource ds = new DataSource(); dele
ListBox
数据。我想从MVVM执行命令
,数据被完全绑定,我不知道为什么它不能与命令
一起工作。单击按钮时,我没有收到消息
视图模型
public class BookmarkViewModel : INotifyPropertyChanged
{
public BookmarkViewModel()
{
DataSource ds = new DataSource();
deleteBookmark = new Command(executeCommand) { Enabled = true };
_bk = ds.getBookmarkDetail();
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
List<BookmarkDetail> _bk;
public List<BookmarkDetail> Bookmarks
{
get { return _bk; }
set
{
if (_bk != value)
{
_bk = value;
OnPropertyChanged("Bookmarks");
}
}
}
private Command deleteBookmark;
public Command DeleteBookmark
{
get
{
return deleteBookmark;
}
set
{
deleteBookmark = value;
}
}
void executeCommand()
{
System.Windows.MessageBox.Show(_bk[0].SuraName);
}
public class Command : ICommand
{
private readonly Action executeAction;
private bool enabled;
public bool Enabled
{
get
{
return enabled;
}
set
{
if (enabled != value)
{
enabled = value;
if (CanExecuteChanged != null)
CanExecuteChanged(this, new EventArgs());
}
}
}
public Command(Action executeAction)
{
this.executeAction = executeAction;
}
public bool CanExecute(object parameter)
{
return enabled;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
executeAction();
}
}
}
公共类BookmarkViewModel:INotifyPropertyChanged
{
公共书签视图模型()
{
数据源ds=新数据源();
deleteBookmark=new命令(executeCommand){Enabled=true};
_bk=ds.getBookmarkDetail();
}
公共事件属性更改事件处理程序属性更改;
公共void OnPropertyChanged(字符串propertyName)
{
if(PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
列表bk;
公共列表书签
{
获取{return}
设置
{
如果(_bk!=值)
{
_bk=值;
OnPropertyChanged(“书签”);
}
}
}
私有命令删除书签;
公共命令删除书签
{
得到
{
返回删除书签;
}
设置
{
deleteBookmark=值;
}
}
void executeCommand()
{
System.Windows.MessageBox.Show(_bk[0].SuraName);
}
公共类命令:ICommand
{
私有只读操作执行;
私人布尔启用;
公共布尔启用
{
得到
{
返回启用;
}
设置
{
如果(已启用!=值)
{
启用=值;
如果(CanExecuteChanged!=null)
CanExecuteChanged(这是新的EventArgs());
}
}
}
公共命令(操作执行)
{
this.executeAction=executeAction;
}
公共布尔CanExecute(对象参数)
{
返回启用;
}
公共事件处理程序CanExecuteChanged;
public void Execute(对象参数)
{
executeAction();
}
}
}
和XAML绑定
<ListBox x:Name="lsbBookmarks" FontFamily="./Fonts/ScheherazadeRegOT.ttf#Scheherazade"
FlowDirection="RightToLeft"
Style="{StaticResource ListBoxStyle1}"
ItemsSource="{Binding Bookmarks}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel HorizontalAlignment="Stretch">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="60"></ColumnDefinition>
</Grid.ColumnDefinitions>
<StackPanel Orientation="Horizontal" Grid.Column="0"
HorizontalAlignment="Stretch">
<TextBlock Padding="20,0,10,0" HorizontalAlignment="Stretch">
<Run FontSize="50" Text="{Binding ArabicText.ArabicAyaNumber}"
FontFamily="./Fonts/KPGQPC.otf#KFGQPC Uthmanic Script HAFS"
Foreground="Blue"/> <Run FontSize="30" Text="{Binding ArabicText.Aya}"/>
</TextBlock>
</StackPanel>
<Button Grid.Column="1" Tag="{Binding ArabicText.ArabicTextID}"
VerticalAlignment="Center"
Height="60" Width="50" HorizontalAlignment="Right"
Content="X" BorderBrush="Red"
Background="Red" BorderThickness="0"
Padding="0" Command="{Binding DeleteBookmark}"></Button>
</Grid>
<Line X1="0" X2="1" Y1="0" Y2="0" Stretch="Fill" VerticalAlignment="Bottom"
StrokeThickness="1" Stroke="LightGray" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
有什么想法吗?如何使用MVVM实现命令
谢谢 您的
DeleteBookmark
命令似乎是作为BookmarkViewModel
类上的属性公开的,而DataTemplate
中用于呈现单个列表框项的实际数据上下文将是BookmarkDetail
的一个实例。由于bookmarkdail
未声明DeleteBookmark
命令,因此绑定失败
要更正此问题,请执行以下操作之一:
BookmarkDetail
类上定义并公开DeleteBookmark
命令,或command=“{binding DataContext.DeleteBookmark,ElementName=lsbBookmarks}”
(未测试)Command=“{binding DataContext.DeleteBookmark,ElementName=lsbBookmarks}”
您还可以使用任何其他ElementName
而不是定义为列表框父项的lsbBookmarks非常感谢,工作顺利!您还可以告诉我,如何将param传递给函数?要在第二种情况下传递参数,您可以使用按钮上的
CommandParameter
属性,并将其绑定到调用它的元素,即当前项的DataContextCommandParameter=“{Binding}”
应该足够了。谢谢,找到了答案。非常感谢,两种方法都对我有效。我会像他之前回答的那样,将@igr2答案标记为解决方案。再次感谢!