آموزش WAF (بررسی Commandها)
نویسنده: مسعود پاکدل
تاریخ: ۱۳۹۳/۰۱/۱۶ ۲۲:۵۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
<Button Content="RemoveItem" Command="{Binding RemoveItemCommand}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75"/>
Command تعریف شده در Button مورد نظر به خاصیتی به نام RemoveItemCommand در BookViewModel که نوع آن ICommand است اشاره میکند. پس باید تغییرات زیر را در ViewModel اعمال کنیم:public ICommand RemoveItemCommand { get; set; } public Book CurrentItem
{
get
{
return currentItem;
}
set
{
if(currentItem != value)
{
currentItem = value;
RaisePropertyChanged("CurrentItem");
}
}
}
private Book currentItem; [Export]
public class BookController
{
[ImportingConstructor]
public BookController(BookViewModel viewModel)
{
ViewModelCore = viewModel;
}
public BookViewModel ViewModelCore
{
get;
private set;
}
public DelegateCommand RemoveItemCommand
{
get;
private set;
}
private void ExecuteRemoveItemCommand()
{
ViewModelCore.Books.Remove(ViewModelCore.CurrentItem);
}
private void Initialize()
{
RemoveItemCommand = new DelegateCommand(ExecuteRemoveItemCommand);
ViewModelCore.RemoveItemCommand = RemoveItemCommand;
}
public void Run()
{
var result = new List<Book>();
result.Add(new Book { Code = 1, Title = "Book1" });
result.Add(new Book { Code = 2, Title = "Book2" });
result.Add(new Book { Code = 3, Title = "Book3" });
Initialize();
ViewModelCore.Books = new ObservableCollection<Models.Book>(result);
(ViewModelCore.View as IBookView).Show();
}
} <DataGrid ItemsSource="{Binding Books}" SelectedItem="{Binding CurrentItem ,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="400" Height="200">
<DataGrid.Columns>
<DataGridTextColumn Header="Code" Binding="{Binding Code}" Width="100"></DataGridTextColumn>
<DataGridTextColumn Header="Title" Binding="{Binding Title}" Width="300"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>