开发者

WPF - MVVM Light, how to bind/set Content of a ContentControl

I'm fairly new to WPF, and I want to create an MVVM based application that use a main window as the shell, have some menu items and when I click those, the Content property of a ContentControl changes.

I added MVVM Light via Nuget, and I now have a ViewModel folder with a ViewModelLocator and the MainViewModel.

My MainWindow.xaml looks like this:

<Window
    x:Class="WpfMvvmApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="470" Width="900" IsTabStop="False" AllowsTransparency="True" Background="{x:Null}" BorderBrush="#FF3F3F3F" PreviewMouseMove="HandlePreviewMouseMove"
    SnapsToDevicePixels="True" TextOptions.TextFormattingMode="Display" TextOptions.TextRenderingMode="ClearType" ResizeMode="NoResize" WindowStyle="None" WindowStartupLocation="CenterOwner"
    DataContext="{Binding Main, Source={StaticResource Locator}}">
    <Grid>
        <Border x:Name="m_edgeBorder" x:FieldModifier="private" Margin="10" Background="White" IsHitTestVisible="False" IsEnabled="False">
            <Border.Effect>
                <DropShadowEffect Opacity="0.999" BlurRadius="16" ShadowDepth="0" />
            </Border.Effect>
        </Border>
        <Grid x:Name="m_contentGrid" x:FieldModifier="private" Background="White" Margin="13">
            <Rectangle Height="28" VerticalAlignment="Top" Fill="White" PreviewMouseDown="HandleHeaderPreviewMouseDown" />
            <Button HorizontalAlignment="Right" Margin="500,6,8,0" VerticalAlignment="To开发者_如何学JAVAp" Style="{StaticResource ChromeButtonStyle}" Click="HandleCloseClick">
                <TextBlock TextWrapping="Wrap" Text="r" FontFamily="Webdings" Foreground="#FF919191" FontSize="13.333" />
            </Button>
        </Grid>
        <ContentControl x:Name="ActiveItem" Content="{Binding CurrentViewModel , Mode=OneWay}" Background="Transparent" HorizontalAlignment="Stretch" IsTabStop="False" Focusable="False" HorizontalContentAlignment="Center"
            VerticalContentAlignment="Center" VerticalAlignment="Bottom" Margin="13,0,12,13" MaxHeight="375" />
    </Grid>
</Window>

Say I have a NetworkView.xaml file, and its view model called NetworkViewModel.

What is the intended use of MVVM Light, to bind the Contentproperty of the ContentControl? I assume you have to set the CurrentViewModel property of the MainViewModel class, but I don't know how the wiring to the view happens, or how I can control it.

Can someone give an example?


You have it correct, the only thing I see missing is a DataTemplate telling the application how to display a NetworkViewModel.

<DataTemplate DataType="{x:Type local:NetworkViewModel}">
    <local:NetworkView />
</DataTemplate>

This would tell WPF that whenever it tries to display a NetworkViewModel, it should use the NetworkView control

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜