开发者

why when setting <ContentControl Content="{Binding CurrentViewModel}" i get the ToString() of the ViewModel name?

开发者 https://www.devze.com 2023-01-03 04:26 出处:网络
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.

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="{Bind开发者_如何学Going 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>


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


I ran into the same issue. My problem was that my view was not bound to my viewmodel correctly.

You must make sure you have something like this declared:

<DataTemplate DataType="{x:Type vma:InlineDepartmentsViewModel}">
    <va:InlineDepartmentsView/>
</DataTemplate>

This was located in my MainSkin.xaml. Also make sure that in your view, you set your binding as well.

DataContext="{Binding InlineDepartments, Source={StaticResource Locator}}"

Where InlineDepartments would be the name of the property in your view locator.


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}"/>

0

精彩评论

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