1

嗨,我在从 xaml 中的集合视图模型绑定数据时遇到问题。

但是数据绑定失败,VisualStudio 报告错误“没有这样的表:配方”有人可以帮我解决这个问题吗?

XAML:

<Page.DataContext>
    <vm:RecipeListViewModel />
</Page.DataContext>

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <ListView Header="Recipes" x:Name="RecipeList" HorizontalAlignment="Left" Height="512" Margin="156,118,0,0" VerticalAlignment="Top" Width="430" DataContext="{Binding Recipes[0].Recipe_Name}" ItemsSource="{Binding RecipeCollection.Recipe}"  />

    <Image x:Name="Img" HorizontalAlignment="Left" Height="260" Margin="698,116,0,0" VerticalAlignment="Top" Width="462"/>
    <Button Content="Button" HorizontalAlignment="Left" Margin="698,420,0,0" VerticalAlignment="Top" Click="Button_Click_1"/>


</Grid>

RecipeListViewModel

    class RecipeListViewModel : INotifyPropertyChanged
{
    ObservableCollection<Recipe> _Recipes;
    public ObservableCollection<Recipe> Recipes
    {
        get { return _Recipes; }
        set 
        {
            if (_Recipes != value)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Recipes"));
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    public RecipeListViewModel()
    {

        _Recipes = new ObservableCollection<Recipe>();

        var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "LCHFData.db");
        using (var db = new SQLite.SQLiteConnection(dbPath))
        {
            var tb = db.Table<Recipe>().ToList<Recipe>();
            foreach (Recipe recipe in tb)
            {
                _Recipes.Add(recipe);
            }
        }

    }
}

食谱视图模型:

 class RecipeViewModel : INotifyPropertyChanged{

    /*
     *   Class to Display whole recipe object
     */
    Recipe _Recipe;
    public Recipe Recipe 
    { 
        set 
        {
            if (_Recipe != value)
            {
                _Recipe = value;
                PropertyChanged(this, new PropertyChangedEventArgs("Recipe"));
            }
        }
        get { return new Recipe() { Recipe_Name = "boo "}; } 
    }

    public event PropertyChangedEventHandler PropertyChanged = delegate { };
}
4

0 回答 0