开发者

How to create and show views on command request?

I've started using mvvm-light toolkit , and new to WPF.

My question is: I want to create an pplication where the main window includes a grid with 2 columns 1 col command and col2 will display views when each command will be pressed. i want the views to be created on command and colsed from it's own view.

I've tried to figure out how to do it but with no success.

I don't know how to write this kind of funconality using the mvvm light.

I just know how to create main window with mainview and another view creted already on load.

Please help me out..

I'm using 2008 WPF3.5

Shirly. Make me happy today.. i've just started the week!!!

ThankU. I have done it and get as a result the Tostring() of the current ViewModel "LU.ViewModel.AllChannelsViewModel" and not the real view.

the View i want to load is : I'm using the mvvm-light

<Grid>
    <G开发者_运维知识库rid.RowDefinitions>
        <RowDefinition Height="30" />
        <RowDefinition Height="30" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <Button Grid.Row="0" Height="22" HorizontalAlignment="Right" Margin="8,4,0,0" Name="closebutton" VerticalAlignment="Top" Width="100" Command="{Binding CloseCommand}">Close</Button>
    <Button Grid.Row="1" Height="22" HorizontalAlignment="Left" Margin="8,4,0,0" Name="button1" VerticalAlignment="Top" Width="100" Command="{Binding GetChannelsCommand}">Load Channels</Button>
    <Button Grid.Row="1" Height="22" HorizontalAlignment="Right" Margin="8,4,0,0" Name="button2" VerticalAlignment="Top" Width="100" Command="{Binding NewChannelCommand}">New Channel</Button>
    <dg:DataGrid   Grid.Row="2"  ItemsSource="{Binding AllChannelsData}" Margin="0,30,0,0" />


</Grid>

What do i miss here? How can i show the real view i want and not the name of the viewmodel?

Shirly


I don't know if there is anything specific to do that with MVVM Light, but a common way to create a view in MVVM is to create a ContentControl that is bound to a property of the ViewModel. When you affect a new ViewModel to that property, the ContentControl renders it using the DataTemplate that matches the ViewModel's type :

<!-- In resources -->
<DataTemplate DataType="{x:Type vm:FooViewModel}">
    <v:FooView />
</DataTemplate>

<DataTemplate DataType="{x:Type vm:BarViewModel}">
    <v:BarView />
</DataTemplate>
...

<!-- In the main view -->

<ContentControl Content="{Binding Current}" />


Please forgive me for giving you an option that is not in MVVM Light toolkit but I have already been down the road that you're on and chose to use Prism for this very reason. I don't have time to go into the details of Prism or event aggregators but in that tool you would:

  1. In your view model create an ICommand that you'd bind your button or events to
  2. Your executed command would call an event aggregator with the method publish. This is an event that you have already set up outside of your VM and is waiting to be called. When called, the event shows your form. You can even pass properties or even other VM's to the event. This allows your VM not to know about the View.

Finally, MVVM has been wonderful for me but it will offer you many challenges such as this. Don't expect to conquer it in one day or create applications quickly but when you get everything in order it is a nice paradigm.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜