How to use Caliburn Micro Event Aggregator in a ViewModel in WPF

740 Views Asked by At

In my UserControl, I'm using a ListCollectionView which contains three parameters. Two of them uses personnalized RichTextBox for parsing HTML. On this control there is two buttons for next/previous item in the collection.

I'm trying to use an Event Aggregator in my ViewModel for two operations:

  1. For the current item, I need to write a parameter in a xml file
  2. For the current item, I need to reinitialize the htmlformatter for the two RichTextBox

The first operation:

    public PopViewModel()
    {
    ...
        AlertData.CollectionChanged += (s, e) => AlertCollectionView.Refresh();
        AlertCollectionView = new ListCollectionView(AlertData);
        AlertCollectionView.MoveCurrentToPosition(0);

    }

    public void PreviousRecordExecute()
    {
        AlertCollectionView.MoveCurrentToPrevious();
    }

    public void NextRecordExecute()
    {
        AlertCollectionView.MoveCurrentToNext();
    }

    private Alert CurrentAlert
    {
        get { return AlertCollectionView.CurrentItem as Alert; }
        set
        {
            AlertCollectionView.MoveCurrentTo(value);
            NotifyOfPropertyChange();
        }
    }

    public void MarkPopAlertAsRead(FeedItem CurrentAlert)
    {
        XElement doc = XElement.Load(diary);
        XElement elementToChange=doc.Descendants("Alert")
            .Single(x=>((int?)x.Element("key")==CurrentAlert.Alertid));
        elementToChange.SetElementValue("IsRead","True");
        doc.save(diary);
    }

MarkPopAlertAsRead is the method I need to associate with Event Aggregator.

The second operation:

public partial class PopupView : UserControl
{
    public PopupView()
    {
        InitializeComponent();
        this.AlertTitleRichTextBox.TextFormatter = new HtmlFormatter();
        this.AlertTxtRichTextBox.TextFormatter = new HtmlFormatter();
    }
}

With this code, If I changed the current item, this two parameters disapear from the screen, so I need to reload Textformatter.

And the view:

<uc:RichTextBox Margin="29,86,31,301" x:Name="AlertTitleRichTextBox" Text="{Binding AlertCollectionView/AlertTitle, UpdateSourceTrigger=PropertyChanged}" BorderThickness="0" Background="{x:Null}"/>
    <TextBox Margin="257,378,10,12" Text="{Binding AlertCollectionView/AlertStartDate, UpdateSourceTrigger=PropertyChanged}" BorderThickness="0" Background="{x:Null}"/>
    <uc:RichTextBox Margin="10,113,10,48" x:Name="AlertTxtRichTextBox" Text="{Binding AlertCollectionView/AlertText, UpdateSourceTrigger=PropertyChanged}" BorderThickness="0" Background="{x:Null}"/>

My wish is to execute theses two operation, when the collection changed(by click on next/previous button). I need some help for implementation, because Caliburn Micro documentation doesn't help me.

0

There are 0 best solutions below