【问题标题】:Command binding throws an exception in the UI命令绑定在 UI 中引发异常
【发布时间】:2012-09-25 18:54:57
【问题描述】:

我正在尝试通过制作魔术卡应用程序来学习 MVVM。从概念上讲,我不知道如何将数据和命令从视图模型链接到视图中。我了解如何在视图模型中提供数据,但不了解如何从视图中访问它。

主要问题是 xaml 中有什么不正确,我在按钮上遇到异常,我假设与命令绑定有关,这也意味着数据可能也不起作用。

<Window x:Class="MagicDB.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MagicDB"
    Title="MainWindow" Width="500" Height="500">

<Window.DataContext>
    <local:MainWindowViewModel x:Name="viewModel" />
</Window.DataContext>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="50*" />
        <RowDefinition Height="450*" />
    </Grid.RowDefinitions>
    <DataGrid AutoGenerateColumns="False" 
        HorizontalAlignment="Left" 
        VerticalAlignment="Top"
        IsSynchronizedWithCurrentItem="True" Width="auto" Height="auto" Grid.Row="1">
        <DataGrid.Columns>
            <DataGridTextColumn Header="ID" Binding="{Binding CardID}" Width="25" ></DataGridTextColumn>
            <DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="110"></DataGridTextColumn>
            <DataGridTextColumn Header="Mana" Binding="{Binding Mana}" Width="30" ></DataGridTextColumn>
            <DataGridTextColumn Header="Card Text" Binding="{Binding CardTXT}" Width="100*"></DataGridTextColumn>
            <DataGridTextColumn Header="Flavor Text" Binding="{Binding FlavorTXT}" Width="100*"></DataGridTextColumn>
            <!--DataGridCheckBoxColumn Header="Cost" Binding="{Binding IsSelected, UpdateSourceTrigger=PropertyChanged}" ></DataGridCheckBoxColumn-->
        </DataGrid.Columns>
    </DataGrid>
    <Button Content="Initiliaze Database" Height="23" HorizontalAlignment="Left" Margin="55,16,0,0" Name="initdb" VerticalAlignment="Top" Width="75"
            Command="Binding InitCardDB"/>
</Grid>

主要问题是按钮上引发了异常。我也不认为我正在获取网格的数据。

namespace MagicDB
{
class MainWindowViewModel : ObservableObject
{
    private CardDB _cards;
    private Command _InitCardDB;

    public CardDB Cards
    {
        get { return _cards; }
        set { _cards = value; OnPropertyChanged("Cards"); }
    }

   public MainWindowViewModel()
    {
        //var cards = GetCards();

        //var cardViewModels = new List<CardViewModel>();
        //cards.ForEach(c => cardViewModels.Add(new CardViewModel(c)));
        _InitCardDB = new Command(InitDB, true);
        Cards = new CardDB(); ;
    }

    public Command InitCardDB
    {
        get
        {
            if (_InitCardDB == null)
            {
                _InitCardDB = new Command(
                    param => InitDB()
                );
            }
            return _InitCardDB;
        }

    }

    private void InitDB()
    {
        _cards = new CardDB();
        _cards.InitDB();
    }
}

}

任何帮助都会很棒,尤其是我做错了什么以及我在概念上犯了错误的地方。我可能完全不正确地处理这个问题?提前感谢您的宝贵时间。

【问题讨论】:

  • 我的项目中唯一的命名空间是“MagicDB”,它用于我的所有文件。
  • 听起来您仍在努力弄清楚DataContext 是什么以及绑定如何工作。您可能对我最近写的一篇博客文章感兴趣,用相当简单的术语解释这些概念:What is this “DataContext” you speak of?
  • 谢谢,瑞秋,这正是我所需要的

标签: wpf c#-4.0 mvvm wpf-controls


【解决方案1】:

DataGrid 没有项目,因为您需要设置它的ItemsSource

Button 导致异常,因为您试图将字符串 "Binding InitCardDb" 分配给 Command 属性而不是使用 "{Binding InitCardDb}" "{}" 告诉 XAML 处理器使用所谓的标记扩展而不是使用正文

除了你的方法看起来是正确的

【讨论】:

  • 谢谢,我应该在哪里设置 ItemsSource? "
    是的,ItemsSource= "{Binding Path=Cards}"
  • 我尝试了两种方法,但仍然无法显示数据。有趣的是,当我执行 ItemsSource="Cards" 时,网格会显示在设计器中,而当我使用 ItemsSource={Binding Cards} 时,它不会,也不起作用。但是按钮现在可以使用了!
  • 如何知道将 CardID 绑定到 Cards[i].CardID?
  • 这就是 ItemsControl 的魔力。它获取 ItemsSource 中的每个项目并以某种方式显示它。 DataGrid 将每个项目转换为 DataGridRow,ListBox 将每个项目转换为 ListBoxItem,ComboBox 将每个项目转换为 ComboBoxItem
猜你喜欢
相关资源
最近更新 更多
热门标签