WPF access controls in a DataTemplate of a ListBox

datatemplatelistboxwpf

I'm building a WPF order manager app and i've run into a situation i'm not sure how to deal with.

I want to re-bind the list of purchase orders requests for each order but i only want to do it if the the purchase order requests panel is visible in the app (they double click the order to show the list of purchase order requests). The problem i have is that the purchase order requests are a listbox inside each list item of the Order listbox and i can't find a way to traverse the controls in the list items (I can only iterate over the actual objects e.g. OrderInfo).

What i would like to do is

OrderListBox.FindName("PurchaseOrderListBox")

An example of the crm with 2 orders showing purchase order requests and 1 order not showing any
alt text http://www.readyflowers.co.uk/images/crm-datatemplate.png

The result i want to achieve
alt text http://www.readyflowers.co.uk/images/crm-datatemplate-saved.png

Best Answer

I'm not sure I understand your problem....

But it feels like you're trying to hack through the UI hierarchy to find something (ala WinForms) - this is like swimming against the tide in WPF. It'll make you tired real fast..

Look around for articles on the ViewModel pattern. One of them that I read is one by Dan Crevier. The basic idea is to have a Model object for the UI - the UI (data)binds to properties exposed by the ViewModel. Now all your UI related logic could go into the ViewModel, which is notified via propertyChanged notifications of any change in session state. So if a field changes, the ViewModel gets notified. The ViewModel updates relevant properties and the UI refreshes accordingly.

In this case ViewModel.OrderInfo.Find_PO_with_X(searchCriteria) will help you get to the right object. If found, you say ViewModel.ShowGreenLight = true and the UI control bound to this property will turn green.

HTH

Related Topic