Noyes - WPF MVVM in Depth Flashcards

1
Q

Set UserControl.DataContext in XAML

A

<usercontrol.datacontext></usercontrol.datacontext>

<customerlistviewmodel></customerlistviewmodel>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

INotifyPropertyChanged ?

A

public class CustomerListViewModel : INotifyPropertyChanged

{

public event PropertyChangedEventHandler PropertyChanged = delegate { };

use:

PropertyChanged(this, new PropertyChangedEventArgs(“Customers”));

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

ObservableCollection in ViewModel

A

public ObservableCollection<customer> Customers</customer>

{

get

{

return _customers;

}

set

{

if (_customers != value)

{

_customers = value;

PropertyChanged(this, new PropertyChangedEventArgs(“Customers”));

}

}

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Window.Resources

declaration DataTemplate DataType

A

<window.resources></window.resources>

<datatemplate></datatemplate>

<customerlistview></customerlistview>

<datatemplate></datatemplate>

<orderview></orderview>

<datatemplate></datatemplate>

<orderprepview></orderprepview>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

DataContext / View-First Construction in code behind

A

<window.datacontext></window.datacontext>

<mainwindowviewmodel></mainwindowviewmodel>

Ctor_code_behind()

{

this.DataContext = new MainWindowViewModel();

InitializeComponent();

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

[CallerMemberName]

A

public class BindableBase : <em>INotifyPropertyChanged</em>

{

public event <em>PropertyChangedEventHandler</em> PropertyChanged = delegate { };

protected virtual void SetProperty<t>(ref T member, T val,</t>

[CallerMemberName] string propertyName = null)

{

if (object.<em>Equals</em>(member, val))

return;

member = val;

OnPropertyChanged();

}

[NotifyPropertyChangedInvocator]

protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)

{

PropertyChanged?.<em>Invoke</em>(this, new <em>PropertyChangedEventArgs</em>(propertyName));

}

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Event declaration

A

public event <em>Action</em><<em>Guid</em>> PlaceOrderRequested = delegate { };

private void OnPlaceOrder(Customer customer)

{

PlaceOrderRequested(customer.Id);

}

  • Use it outside:
  • _customerListViewModel.PlaceOrderRequested += NavToOrder;

private void NavToOrder(<em>Guid</em> customerId)

{

_orderViewModel.CustomerId = customerId;

CurrentViewModel = _orderViewModel;

*

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

ViewModelLocator

A

<usercontrol></usercontrol>

xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”

xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”

xmlns:mc=”http://schemas.openxmlformats.org/markup-compatibility/2006”

xmlns:d=”http://schemas.microsoft.com/expression/blend/2008”

xmlns:local=”clr-namespace:MVVMHookupDemo.Customers”

xmlns:root=”clr-namespace:MVVMHookupDemo”

mc:Ignorable=”d”

d:DesignHeight=”450” d:DesignWidth=”800”

root:ViewModelLocator.AutoWireViewModel=”True”

>

<grid></grid>

public static class ViewModelLocator

{

public static readonly <em>DependencyProperty</em> AutoWireViewModelProperty = <em>DependencyProperty</em>.<em>RegisterAttached</em>(“AutoWireViewModel”,

typeof(bool),

typeof(ViewModelLocator),

new <em>PropertyMetadata</em>(default(bool),

AutoWireViewModelChanged));

// d is the element on this attached property is being set

private static void AutoWireViewModelChanged(<em>DependencyObject</em> d, <em>DependencyPropertyChangedEventArgs</em> e)

{

if (<em>DesignerProperties</em>.<em>GetIsInDesignMode</em>(new <em>DependencyObject</em>())) return;

// get view type name

var viewTypeName = d.<em>GetType</em>().<em>FullName</em>;

var viewModelTypeName = viewTypeName + “Model”;

var viewModelType = Type.<em>GetType</em>(viewModelTypeName);

if (viewModelType != null)

{

var viewModel = <em>Activator</em>.<em>CreateInstance</em>(viewModelType);

((<em>FrameworkElement</em>)d).<em>DataContext</em> = viewModel;

}

}

public static bool GetAutoWireViewModel(<em>DependencyObject</em> obj)

{

return (bool) obj.<em>GetValue</em>(AutoWireViewModelProperty);

}

public static void SetAutoWireViewModel(<em>DependencyObject</em> obj, bool value)

{

obj.<em>SetValue</em>(AutoWireViewModelProperty, value);

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Data Binding

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
A

<grid></grid>

<datagrid></datagrid>

ItemsSource=”{Binding Customers}”

AutoGenerateColumns=”False”

EnableRowVirtualization=”True”

RowDetailsVisibilityMode=”VisibleWhenSelected”

SelectedItem=”{Binding SelectedCustomer}” >

<datagrid.columns></datagrid.columns>

<datagridtextcolumn></datagridtextcolumn>

<datagridtextcolumn></datagridtextcolumn>

<datagridtextcolumn></datagridtextcolumn>

public class CustomerListViewModel

{

readonly ICustomersRepository _repo = new CustomersRepository();

private <em>ObservableCollection</em><customer> _customers;</customer>

public CustomerListViewModel()

{

if (<em>DesignerProperties</em>.<em>GetIsInDesignMode</em>(new <em>DependencyObject</em>()))

return;

Customers = new <em>ObservableCollection</em><customer>(_repo.GetCustomersAsync().<em>Result</em>);</customer>

}

public <em>ObservableCollection</em><customer> Customers</customer>

{

get => _customers;

set => _customers = value;

}

}

public class Customer

{

public Customer()

{

Orders = new <em>List</em><order>();</order>

}

[<em>Key</em>]

public <em>Guid</em> Id { get; set; }

public <em>Guid</em>? StoreId { get; set; }

public string FirstName { get; set; }

public string LastName { get; set; }

public string FullName { get { return FirstName + “ “ + LastName; } }

public string Phone { get; set; }

public string Email { get; set; }

public string Street { get; set; }

public string City { get; set; }

public string State { get; set; }

public string Zip { get; set; }

public <em>List</em><order> Orders { get; set; }</order>

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly