2

Ok, well I am pretty pretty pretty new to WPF and XAML, despite my search I could not find a simple solution and it seems to me that I won't be able to find an answer pretty soon.

The question is so simple, I have created a WPF project and have a datagrid in SelectList.xaml Once a row selected, I save the selected row in an object say this object called "category". So far everything is ok but I can't figure out how I am going to obtain a reference to this object from an other place temp.xaml ?

Thanks very much Any help will be highly appreciated Cheers

Pinchy
  • 1,506
  • 8
  • 28
  • 49
  • Can you post your XAML, it is not entirely clear how you plan for your 2 views (SelectList.xaml and temp.xaml) to interact. – Oppositional Dec 01 '10 at 20:20

2 Answers2

2

A common way to provide indirect communication in WPF is to leverage the Mediator pattern. You can use a mediator to publish the selection of your category, and have the temp view subscribe to notification of a change in selection of your category.

See http://www.eggheadcafe.com/tutorials/aspnet/ec832ac7-6e4c-4ea8-81ab-7374d3da3425/wpf-and-the-model-view-vi.aspx for a simple example of a concrete mediator. There are also several popular MVVM frameworks available that provide Mediator pattern implementations if you want a more robust implementation.

Simple Mediator implementation:

public sealed class Mediator
{
    private static Mediator instance = new Mediator();
    private readonly Dictionary<string, List<Action<object>>> callbacks 
      = new Dictionary<string, List<Action<object>>>();

    private Mediator() { }

    public static Mediator Instance
    {
        get
        {
            return instance;
        }
    }

    public void Register(string id, Action<object> action)
    {
        if (!callbacks.ContainsKey(id))
        {
            callbacks[id] = new List<Action<object>>();
        }

        callbacks[id].Add(action);
    }

    public void Unregister(string id, Action<object> action)
    {
        callbacks[id].Remove(action);

        if (callbacks[id].Count == 0)
        {
            callbacks.Remove(id);
        }
    }

    public void SendMessage(string id, object message)
    {
        callbacks[id].ForEach(action => action(message));
    }
}

SelectList.xaml code-behind:

private void DataGrid_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
    var category = e.AddedItems.FirstOrDefault() as Category;

    if(category != null)
    {
        Mediator.Instance.SendMessage("Category Selected", category);
    }
}

Temp.xaml code-behind:

public Temp()
{
  InitializeComponent();

  Mediator.Instance.Register
  (
      "Category Selected",
      OnCategorySelected
  );
}

private void OnCategorySelected(object parameter)
{
  var selectedCategory = parameter as Category;

  if(selectedCategory != null)
  {
  }
}
Oppositional
  • 11,141
  • 6
  • 50
  • 63
  • That is sure a better way of doing it. +1 :) –  Dec 02 '10 at 05:05
  • Excellent! Tank you so much, much appreciated. – Pinchy Dec 02 '10 at 11:15
  • 2
    One word of caution, the mediator described above isn't using a weak action, so memory leaks might be possible. Josh Smith has an articale here: http://joshsmithonwpf.wordpress.com/2009/04/06/a-mediator-prototype-for-wpf-apps/ – Oppositional Dec 03 '10 at 00:59
0

Create a accessibly method (public if you want to) which accepts the reference of this "category" object in the "Temp.xaml" code behind file. Then pass the "category" object from the "SelectList.xaml" code behind file to the "Temp.xaml" file through this method.

  • Ok I got that, I have created a public setter in temp.xaml, would you tell me how I can get a reference to temp.xaml so I can send the object there? I don't know what the instance of temp.xaml called during runtime! – Pinchy Dec 01 '10 at 20:31
  • Ok, you can deal with it 2 ways. Either you can make the property in the Temp.xaml as static and handle your reference of category there. Or you need to find a way to pass the Temp.xaml class instance reference back to SelectList.xaml when a new instance of Temp.xaml is created. –  Dec 01 '10 at 20:33