views:

49

answers:

2

Hi , I'm using mvvm-light. i;m trying to create an application with one side command button and the other side place holder for views.

when i try to create the view by command i get the ToString name of the ViewModel.

For Example : LU.ViewModel.ChannelsViewModel

What i'm missing?

here is my code:

mainWindow

New Channel

        <ContentControl x:Name="_placeholder"                               
                        x:FieldModifier="private"
                            Margin="16"
                            HorizontalContentAlignment="Center"
                            VerticalContentAlignment="Center"
                            Content="{Binding CurrentViewModel , Mode=OneWay}"/>

         </StackPanel>

resources:

of the CustomerViewModel class shown in the main window. -->

ChannelView

<Grid Margin="4">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="6" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="30" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="30" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="30" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="30" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <Grid.Resources>
        <DataTemplate DataType="{x:Type ValidationError}">
            <TextBlock 
      FontStyle="Italic"
      Foreground="Red"
      HorizontalAlignment="Right"
      Margin="0,1"
      Text="{Binding Path=ErrorContent}" 
      />
        </DataTemplate>
    </Grid.Resources>

    <!-- NAME-->
    <Label 
  Grid.Row="0" Grid.Column="0" 
  Content="Name:" 
  HorizontalAlignment="Right"
  Target="{Binding ElementName=NameTxt}"
  />
    <TextBox 
  x:Name="NameTxt"
  Grid.Row="0" Grid.Column="2" 
  Text="{Binding ChannelName, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" 
  Validation.ErrorTemplate="{x:Null}"
  />
    <ContentPresenter 
  Grid.Row="1" Grid.Column="2"
  Content="{Binding ElementName=NameTxt, Path=(Validation.Errors).CurrentItem}"
  />

    <!-- IP-->
    <Label 
  Grid.Row="2" Grid.Column="0" 
  Content="IP:" 
  HorizontalAlignment="Right"
  Target="{Binding ElementName=IPTxt}" 
  />
    <TextBox 
  x:Name="IPTxt"
  Grid.Row="2" Grid.Column="2" 
  Text="{Binding IP, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
  Validation.ErrorTemplate="{x:Null}"
  />
    <ContentPresenter 
  Grid.Row="3" Grid.Column="2"
  Content="{Binding ElementName=IPTxt, Path=(Validation.Errors).CurrentItem}"
  />

    <!-- Control Port-->
    <Label 
  Grid.Row="4" Grid.Column="0" 
  Content="Control port:" 
  HorizontalAlignment="Right"
  Target="{Binding ElementName=controlPortTxt}" 
  />
    <TextBox 
  x:Name="controlPortTxt"
  Grid.Row="4" Grid.Column="2" 
  Text="{Binding ControlPort, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
  Validation.ErrorTemplate="{x:Null}"
  />
    <ContentPresenter 
  Grid.Row="5" Grid.Column="2"
  Content="{Binding ElementName=controlPortTxt, Path=(Validation.Errors).CurrentItem}"
  />

    <!-- data Port-->
    <Label 
  Grid.Row="6" Grid.Column="0" 
  Content="Data port:" 
  HorizontalAlignment="Right"
  Target="{Binding ElementName=dataPortTxt}" 
  />
    <TextBox 
  x:Name="dataPortTxt"
  Grid.Row="6" Grid.Column="2" 
  Text="{Binding DataPort, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
  Validation.ErrorTemplate="{x:Null}"
  />
    <ContentPresenter 
  Grid.Row="7" Grid.Column="2"
  Content="{Binding ElementName=dataPortTxt, Path=(Validation.Errors).CurrentItem}"
  />

    <!-- SAVE BUTTON -->
    <Button 
  Grid.Row="8" Grid.Column="2"
  Command="{Binding SaveCommand}"
  Content="_Save"
  HorizontalAlignment="Right"
  Margin="4,2" 
  MinWidth="60" 
  />
</Grid>

A: 

You're always going to get that if you set the "Content" property with a complex object (ex: a ViewModel).

Instead of binding your ViewModel to the "Content" bind it to the "DataContext" instead.

If you really want to bind it to Content then your gonna have to bind to a property of your ViewModel and not just the ViewModel itself ex:

Content="{Binding CurrentViewModel.SomeStringPropertyInMyVM , Mode=OneWay}"/>

vidalsasoon
+1  A: 

that works for me, but im not sure if its a good practice:

  <UserControl.Resources>
        <DataTemplate DataType="{x:Type vm:SimpleReflectionViewModel}">
            <view:SimpleReflecionView></view:SimpleReflecionView>
        </DataTemplate>
  </UserControl.Resources>

<ContentControl Margin="4,0,4,4" HorizontalAlignment="Center" VerticalAlignment="Bottom" Content="{Binding CurrentViewModel}" /> 

//CurrentViewModel is a property of type ViewModelBase

Roberto