How to dispose Interaction.Behaviors in WPF
-
I'm using WPF MVVM in my project. I have used Interaction.Behaviors in the root section of one of my views:
One of the Behavior classes is:
public class SfDataPagerBehavior_File : Behavior, IRecipient, IRecipient
{
public SfDataPagerBehavior_File()
{
WeakReferenceMessenger.Default.Register(this);
WeakReferenceMessenger.Default.Register(this);
}
protected override void OnAttached()
{
base.OnAttached();
.
.
AssociatedObject.dataPager_file.OnDemandLoading += OnOnDemandLoading;
//Some codes
.
.
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.dataPager_file.OnDemandLoading -= OnOnDemandLoading;
}
public async void Receive(FileFilterText message)
{
//Some codes
}
public async void Receive(UpdateDatagrid_Pdf message)
{
//Some codes
}
}I want to dispose all Interaction.Behaviors in the view by pressing a button. How can I do this?
-
I'm using WPF MVVM in my project. I have used Interaction.Behaviors in the root section of one of my views:
One of the Behavior classes is:
public class SfDataPagerBehavior_File : Behavior, IRecipient, IRecipient
{
public SfDataPagerBehavior_File()
{
WeakReferenceMessenger.Default.Register(this);
WeakReferenceMessenger.Default.Register(this);
}
protected override void OnAttached()
{
base.OnAttached();
.
.
AssociatedObject.dataPager_file.OnDemandLoading += OnOnDemandLoading;
//Some codes
.
.
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.dataPager_file.OnDemandLoading -= OnOnDemandLoading;
}
public async void Receive(FileFilterText message)
{
//Some codes
}
public async void Receive(UpdateDatagrid_Pdf message)
{
//Some codes
}
}I want to dispose all Interaction.Behaviors in the view by pressing a button. How can I do this?
That's one of the traps of MVVM: over-engineering in order to fit the app to the pattern. It presumes one is incapable of coming up with something better on their own for their app's needs.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
-
That's one of the traps of MVVM: over-engineering in order to fit the app to the pattern. It presumes one is incapable of coming up with something better on their own for their app's needs.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I