【问题标题】:WPF BindingError 40, yet program worksWPF BindingError 40,但程序有效
【发布时间】: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


    【解决方案1】:
    <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>
    

    这应该可以。基本上你得到了错误,因为它找不到属性 TransformScaleY/TransformScaleX 的路径。由于视图模型正在寻找要在当前视图上绑定的属性。这也与混合 mvvm 范例 viewmodel first / view first 有关。只需要确切地知道你希望它如何与其他所有东西一起玩。

    【讨论】:

      【解决方案2】:

      当 DataContext 是 GameViewModel 而不是 PlayerViewModel 时,第一条错误消息表明您正在尝试绑定到 TransformScaleX 属性。属性TransformScaleX 的第二条错误消息指出了类似的事情。

      尝试检查此问题中发布的第二个UserControl。当您期望它是 PlayerViewModel 时,它似乎是 DataContextGameViewModel 对象。

      【讨论】:

      • 我尝试将PlayerViewDataContext 设置为DataContext="MyNamespace.ViewModels.PlayerViewModel",错误将单词GameViewModel 更改为String,程序仍然加载但停止正常运行.
      • 您可以为PlayerView 发布 XAML 吗?
      • XAML 的最低位是PlayerView。抱歉,如果不清楚。我省略了命名空间声明,&lt;UserControl.Resources /&gt;&lt;Image /&gt;
      猜你喜欢
      • 2013-04-29
      • 2014-05-05
      • 1970-01-01
      • 2016-08-10
      • 1970-01-01
      • 1970-01-01
      • 2021-07-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多