Sql, Wpf, Xaml, C#, Binding data, Dynamic resource, accessing to non-static data, Obtaining a Reference to an Object -
ok, pretty pretty pretty new wpf , xaml, despite search not find simple solution , seems me won't able find answer pretty soon.
the question simple, have created wpf project , have datagrid in selectlist.xaml once row selected, save selected row in object object called "category". far ok can't figure out how going obtain reference object other place temp.xaml ?
thanks highly appreciated cheers
a common way provide indirect communication in wpf leverage mediator pattern. can use mediator publish selection of category, , have temp view subscribe notification of change in selection of category.
see http://www.eggheadcafe.com/tutorials/aspnet/ec832ac7-6e4c-4ea8-81ab-7374d3da3425/wpf-and-the-model-view-vi.aspx simple example of concrete mediator. there several popular mvvm frameworks available provide mediator pattern implementations if want 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 { { 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() 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 category; if(selectedcategory != null) { } }
Comments
Post a Comment