0

ANSWER: Basically, I wasn't using Caliburn Micro properly. Using ContentControl was the proper way to bind my views and models. More here: Does Caliburn.Micro play nicely with user controls?

I'm using WPF with the Caliburn Micro framework. The program starts up with the GameViewModel.cs, which correctly syncs itself with the GameView.xaml.

From GameViewModel.cs I create four new PlayerViewModel(), which sync correctly with PlayerView.xaml. In PlayerView.xaml I have a couple of bindings, everything all binds correctly, no problems, is updated just as expected and as I want..

But in the Output window, I'm getting all kinds of errors that the Bindings from PlayerView can't bind to GameViewModel.cs.

Now, great that the program works as expected, but all these errors made me worry enough to check what I'm doing wrong. Please help me out, what's going on here?

(My guess is that it's Caliburn related, and it tries to bind the x:Name to something in GameViewModel because that's the startup project. If I -do- happen to be on the mark, how do I change it to bind properly?)

Error as follows:

System.Windows.Data Error: 40 : BindingExpression path error: 'TransformScaleX' property not found on 'object' ''GameViewModel' (HashCode=24649639)'. BindingExpression:Path=TransformScaleX; DataItem='GameViewModel' (HashCode=24649639); target element is 'TextBlock' (Name='ScaleX'); target property is 'Text' (type 'String')

System.Windows.Data Error: 40 : BindingExpression path error: 'TransformScaleY' property not found on 'object' ''GameViewModel' (HashCode=24649639)'. BindingExpression:Path=TransformScaleY; DataItem='GameViewModel' (HashCode=24649639); target element is 'TextBlock' (Name='ScaleY'); target property is 'Text' (type 'String')

List continues for a bit, same error for other properties too. Code as below, I trimmed out (seemingly?) irrelevant bits:

//This works fine
public class GameViewModel : PropertyChangedBase
{
    private Game _model;
    public GameViewModel() { _model = new Game(this); }

    private PlayerViewModel _player1 = new PlayerViewModel("player1");
    public PlayerViewModel Player1 { get { return _player1; } set { _player1 = value; NotifyOfPropertyChange("Player1"); } }
}

//This works fine too.
<UserControl>
    <Grid Width="1280" Height="720" Background="Cyan">
        <local:PlayerView cal:View.Model="{Binding Player1}" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,220,0,0"/>
    </Grid>
</UserControl>

//Works fine
public class PlayerViewModel : PropertyChangedBase
{
    private Player _model;
    public PlayerViewModel(string name)
    {
        Username = name;
        _model = new Player(this);
    }

    //NotifyOfPropertyChange is handled properly elsewhere
    public string TransformScaleX { get; set; } 
    public string TransformScaleY { get; set; }
}

//All binds with no issues.
<UserControl>
    <Grid Width="288" Height="55">
        <TextBlock x:Name="ScaleX" Visibility="Hidden" Text="{Binding TransformScaleX}"/>
        <TextBlock x:Name="ScaleY" Visibility="Hidden" Text="{Binding TransformScaleY}"/>
    </Grid>
</UserControl>
Community
  • 1
  • 1
Taelia
  • 591
  • 3
  • 20

2 Answers2

0

The 1st error message indicates that you're trying to bind to TransformScaleX property when DataContext is GameViewModel instead of PlayerViewModel. Similar thing indicated by 2nd error message for property TransformScaleX.

Try to check the 2nd UserControl posted in this question. It seems that it's DataContext is a GameViewModel object when you expect it to be a PlayerViewModel.

har07
  • 88,338
  • 12
  • 84
  • 137
  • I've attempted to set the `DataContext` of the `PlayerView` to `DataContext="MyNamespace.ViewModels.PlayerViewModel"`, the error changed the word `GameViewModel` to `String`, and the program still loads but stops functioning correctly. – Taelia Mar 20 '14 at 04:37
  • Can you post XAML for `PlayerView`? – har07 Mar 20 '14 at 05:19
  • The bottom-most bit of XAML is the `PlayerView`. Sorry if that wasn't clear. I left out namespace declarations, `` and an ``. – Taelia Mar 20 '14 at 20:36
0
<UserControl>
    <Grid Width="1280" Height="720" Background="Cyan">
        <ContentControl cal:View.Model="{Binding Player1}" cal:View.Content="PlayerView" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,220,0,0"/>
    </Grid>
</UserControl>

this should work. basically your getting the error because it can't find the path to the properties TransformScaleY/TransformScaleX. Since the viewmodel is looking for properties to bind to on the current view. This also has to do with mixing mvvm paradigms viewmodel first / view first. just have to know exactly how you want it to play with everything else.

mvermef
  • 3,814
  • 1
  • 23
  • 36