【发布时间】:2014-04-26 16:41:24
【问题描述】:
回答:基本上,我没有正确使用 Caliburn Micro。 使用 ContentControl 是绑定我的视图和模型的正确方法。 更多内容:Does Caliburn.Micro play nicely with user controls?
我将 WPF 与 Caliburn Micro 框架一起使用。 程序以 GameViewModel.cs 启动,它与 GameView.xaml 正确同步。
我从 GameViewModel.cs 创建了四个 new PlayerViewModel(),它们与 PlayerView.xaml 正确同步。在 PlayerView.xaml 我有几个绑定,一切都正确绑定,没有问题,按预期和我想要的那样更新..
但是在输出窗口中,我收到了来自 PlayerView 的绑定无法绑定到 GameViewModel.cs 的各种错误。
现在,该程序按预期运行很好,但所有这些错误让我非常担心检查我做错了什么。请帮帮我,这是怎么回事?
(我的猜测是它与 Caliburn 相关,它尝试将 x:Name 绑定到 GameViewModel 中的某个东西,因为那是启动项目。如果我 - 确实 - 碰巧在标记上,我该如何将其更改为绑定正确吗?)
错误如下:
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 会持续一段时间,其他属性也会出现同样的错误。 代码如下,我剪掉了(看起来?)不相关的位:
//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>
【问题讨论】:
标签: c# wpf xaml caliburn.micro