PropertyMetadata has the following signature
new PropertyMetadata (false,OnChanged)
private void OnChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){}
It can be uses to inject code
(1) ViewModelLocator --- Wire up View DataContext to a ViewModel.
//just as normal Dependency property declaration
public static readonly DependencyProperty AutoWireViewModelProperty =
DependencyProperty.Register( "AutoWireViewModel", typeof(bool),
typeof(VMLocators), new PropertyMetadata(false,OnChanged));
// then create ViewModel as DataContext
void OnChanged(DependencyObj d,PropChangedEventArgs e) {
var vmType=d.GetType().FullName+"Model" //convention
(FrameworkElement)d.DataContext = Activator.GetInstance(vmType);
}
usage in xmal
<UserControl ... local:VMLocators.AutoWireViewModel="true" >
(2) Similarly as in (1) Loaded Event can trigger method calls through attached property
Similarly for Attached Property Behavior
<UserControl loc:LoadedMethodName="GetDataAsync" />
var f=d as FrameworkElement;
f.Loaded+=() =>{
MethodInfo mi=d.GetType.GetMethod(e.NewValue);
mi.Invoke(vm,null);
}
ViewModel has async void GetDataAsync() { .. var data= await GetDataAsync(); ...}
(3) WPF regular behavior also has PropertyMeta ChangedEvent and can pop Notification
ShowNotificationMsgBehevior : Behavior
declare dependency property Message as string
void OnMsg(d,e) {
var b=d as ShowNotificationMsgBehevior ;
b.AssocObj.Content=e.NewValue}
<ContentControl>
<interaction.ShowNotificationMsgBehevior Message={Binding NotifMsg} />
Wednesday, March 16, 2016
WPF PropertyMetadata Change Event to inject code
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment