ChildWindow Loading

Mar 27, 2011 at 1:56 PM

Hi Guys,

Currently I use a relay to launch a command from a datagrid as follows

        						<Image x:Name="image" Tag="{Binding Id}"  Source="/Sample;component/images/Video.png" Margin="5" Stretch="UniformToFill" Loaded="image_Loaded">
        									<i:EventTrigger EventName="MouseLeftButtonUp">
        								 	  <n:ExecuteCommandAction Command="{StaticResource LoadVideoWindowRelay}"
        											Parameter="{Binding Tag, ElementName=image}" />						                      

The command executes and the following is run.

   LoadVideoCommand = new ActionCommand<int>((s) => 
                    currentID = s;                    
                    NavigationService.Navigate(new nRoute.Navigation.NavigationRequest("Views/Video/", NavigateMode.New),  NavigationService.GetNavigationHandler("PopupContainer") );                                                           
                }, true);

I don't like Navigating from the ViewModel but as both the parent view and Video view (popup but not model) share the same ViewModel I can't see a way to pass dependency parameters using NavigateAction.

So I guess I have two questions here.

1. Am I missing something, I could have 2 ViewModels but it feels odd as both views rely on the same data.

2. How would I go about closing a child view by a) clicking on the same image (ie Toggle on/off)   b) close button within the popup.   I'm wanting to ensure some handlers are disposed of but should they click on another image (ie another row) I don't really need the window to close just tell the popup the new ID.

Hope this makes sense

Many Thanks



Apr 11, 2011 at 11:16 AM

Hi Jeremy, sorry for the delay in replying back. Now, as for question 1. well, the normal way to pass an argument is to do something like:

<n:ReverseCommandTrigger ReverseCommand="{Binding ExclusionsReverseCommand}">
	<n:NavigateAction Url="Common/DayTimeExclusions/">
		<n:DependencyParameter Key="DayTimeSet" Value="{Binding SelectedDayTime}" />

However, incase the parameter comes from elsewhere (basically from outside the scope of your DataTemplate) you could use a ValueRelay:

<!-- DECLARE -->
	<n:ValueRelay x:Key="DayTimeSetRelay" />

<!-- BRIDGE -->
	<n:BridgeValueBehavior ValueRelay="{StaticResource DayTimeSetRelay}" ValueSource="{Binding DayTimeSet}" />
<!-- USE -->
<n:ReverseCommandTrigger ReverseCommand="{Binding ExclusionsReverseCommand}">
	<n:NavigateAction Url="Common/DayTimeExclusions/">
		<n:DependencyParameter Key="DayTimeSet" Value="{Binding Value, Source={StaticResource DayTimeSetRelay}}" />

And just FYI, at times I've put my VM into a ValueRelay - that allows you to share the VM or pass it around.

As for Question 2, I'm not 100% sure what you are trying to do but the way I normally set up popups is to use this concept of a Task and a TaskParent, consider:


public interface ITask
	event EventHandler Completed;

	ICommand CommitCommand { get; }

	ICommand CancelCommand { get; }

	bool IsCancellable { get;  }

public interface ITaskParent
	void OnCompleted(bool? completed, Object response);

Now when a Task (normally a popup) is spawned it's given a parent object (ITaskParent) that it responds back to - this way you can hook up the necessary handlers. Also see the other approach of using IDisposable tokens with popups here

Hope this helps,