列表框C#XAML(Azure移动服务)UWP的数据绑定列表

列表框C#XAML(Azure移动服务)UWP的数据绑定列表,c#,xaml,listbox,uwp,azure-mobile-services,C#,Xaml,Listbox,Uwp,Azure Mobile Services,所以我有一个在线数据库。我希望查询结果显示在列表框中。我试图使用数据绑定,但我认为我完全错了 档案 [![在此处输入图像描述][1][1] 桌子 MainViewModel.cs namespace Test { class MainViewModel { IMobileServiceTable<ProductTable> product = App.MobileService.GetTable<ProductTable>();

所以我有一个在线数据库。我希望查询结果显示在列表框中。我试图使用数据绑定,但我认为我完全错了

档案

[![在此处输入图像描述][1][1]

桌子

MainViewModel.cs

namespace Test
{
    class MainViewModel
    {
        IMobileServiceTable<ProductTable> product = App.MobileService.GetTable<ProductTable>();

        //In this method, load your data into Products
        public async void Load()
        {
            // This query filters out completed TodoItems.
            MobileServiceCollection<ProductTable, ProductTable> Products = await product
                .Where(ProductTable => ProductTable.Price == 15)
                .ToCollectionAsync();

            // itemsControl is an IEnumerable that could be bound to a UI list control
            IEnumerable itemsControl = Products;
        }
    }
}
public class MainViewModel : INotifyPropertyChanged
{
    IMobileServiceTable<ProductTable> product = App.MobileService.GetTable<ProductTable>();

    public List<ProductTable> Products { get; private set; }

    public event PropertyChangedEventHandler PropertyChanged;

    public async void Load()
    {
        Products = await product
            .Where(ProductTable => ProductTable.Price == 15)
            .ToListAsync();

        //Notify that the property has changed to alert to UI to update.
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Products)));
    }
}
XAML.cs 您不需要声明新的列表框,只需使用itemsource

private async void button1_Click(object sender, RoutedEventArgs e)
{
    IMobileServiceTable<ProductTable> productTest = App.MobileService.GetTable<ProductTable>();

    // This query filters out completed TodoItems.
    MobileServiceCollection<ProductTable, ProductTable> products = await productTest
        .Where(ProductTable => ProductTable.Type == "Test")
        .ToCollectionAsync();

    lb.ItemsSource = products;

}
private async void按钮1\u单击(对象发送方,路由目标)
{
IMobileServiceTable productTest=App.MobileService.GetTable();
//此查询过滤掉已完成的TodoItems。
MobileServiceCollection产品=等待产品测试
.Where(ProductTable=>ProductTable.Type==“测试”)
.ToCollectionAsync();
lb.ItemsSource=产品;
}
XAML

在STACKPANEL中,切勿使用高度“*”这将导致严重错误

<ListBox x:Name="lb" Margin="10,10,10,100" >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Width="300">
                <TextBlock Text="{Binding Name}"  FontSize="10"></TextBlock>
                <TextBlock Text="{Binding Title}" FontSize="10"></TextBlock>
                <TextBlock Text="{Binding Description}" FontSize="10"></TextBlock>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

您正在创建两个列表框—一个在未显示的代码中,另一个在绑定到DataContext的XAML中

删除代码中的最后两行,并将XAML更改为:

ItemsSource="{Binding Products}"
接下来,将产品作为类的属性公开(如果要使用MVVM,请使用ViewModel),并确保将ListBox的DataContext设置为所述ViewModel/类

我不熟悉MobileServiceCollection,所以我认为它是可绑定的。如果不是,则将产品公开为可观察集合(如果其中的值随时间变化)或任何其他受支持的集合类型

关于属性的详细说明

创建一个类:

public class MainViewModel {
    //This is a property
    public ObservableCollection<ProductTable> Products { get; set; }

    //In this method, load your data into Products
    public void Load(){ 
        //Products = xxx
    }
}
MainViewModel.cs

namespace Test
{
    class MainViewModel
    {
        IMobileServiceTable<ProductTable> product = App.MobileService.GetTable<ProductTable>();

        //In this method, load your data into Products
        public async void Load()
        {
            // This query filters out completed TodoItems.
            MobileServiceCollection<ProductTable, ProductTable> Products = await product
                .Where(ProductTable => ProductTable.Price == 15)
                .ToCollectionAsync();

            // itemsControl is an IEnumerable that could be bound to a UI list control
            IEnumerable itemsControl = Products;
        }
    }
}
public class MainViewModel : INotifyPropertyChanged
{
    IMobileServiceTable<ProductTable> product = App.MobileService.GetTable<ProductTable>();

    public List<ProductTable> Products { get; private set; }

    public event PropertyChangedEventHandler PropertyChanged;

    public async void Load()
    {
        Products = await product
            .Where(ProductTable => ProductTable.Price == 15)
            .ToListAsync();

        //Notify that the property has changed to alert to UI to update.
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Products)));
    }
}
public类主视图模型:INotifyPropertyChanged
{
IMobileServiceTable product=App.MobileService.GetTable();
公共列表产品{get;private set;}
公共事件属性更改事件处理程序属性更改;
公共异步无效加载()
{
产品=等待产品
.Where(ProductTable=>ProductTable.Price==15)
.ToListAsync();
//通知属性已更改为向要更新的UI发出警报。
PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(产品名称));
}
}
您需要将ProductTable公开,否则将出现编译错误

希望上述内容能够让您使用上述ItemsSource绑定来绑定ListBox


请注意,上面的代码不一定是最佳实践。

您正在创建两个列表框-一个在代码中没有显示,另一个在XAML中,绑定到您的DataContext

删除代码中的最后两行,并将XAML更改为:

ItemsSource="{Binding Products}"
接下来,将产品作为类的属性公开(如果要使用MVVM,请使用ViewModel),并确保将ListBox的DataContext设置为所述ViewModel/类

我不熟悉MobileServiceCollection,所以我认为它是可绑定的。如果不是,则将产品公开为可观察集合(如果其中的值随时间变化)或任何其他受支持的集合类型

关于属性的详细说明

创建一个类:

public class MainViewModel {
    //This is a property
    public ObservableCollection<ProductTable> Products { get; set; }

    //In this method, load your data into Products
    public void Load(){ 
        //Products = xxx
    }
}
MainViewModel.cs

namespace Test
{
    class MainViewModel
    {
        IMobileServiceTable<ProductTable> product = App.MobileService.GetTable<ProductTable>();

        //In this method, load your data into Products
        public async void Load()
        {
            // This query filters out completed TodoItems.
            MobileServiceCollection<ProductTable, ProductTable> Products = await product
                .Where(ProductTable => ProductTable.Price == 15)
                .ToCollectionAsync();

            // itemsControl is an IEnumerable that could be bound to a UI list control
            IEnumerable itemsControl = Products;
        }
    }
}
public class MainViewModel : INotifyPropertyChanged
{
    IMobileServiceTable<ProductTable> product = App.MobileService.GetTable<ProductTable>();

    public List<ProductTable> Products { get; private set; }

    public event PropertyChangedEventHandler PropertyChanged;

    public async void Load()
    {
        Products = await product
            .Where(ProductTable => ProductTable.Price == 15)
            .ToListAsync();

        //Notify that the property has changed to alert to UI to update.
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Products)));
    }
}
public类主视图模型:INotifyPropertyChanged
{
IMobileServiceTable product=App.MobileService.GetTable();
公共列表产品{get;private set;}
公共事件属性更改事件处理程序属性更改;
公共异步无效加载()
{
产品=等待产品
.Where(ProductTable=>ProductTable.Price==15)
.ToListAsync();
//通知属性已更改为向要更新的UI发出警报。
PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(产品名称));
}
}
您需要将ProductTable公开,否则将出现编译错误

希望上述内容能够让您使用上述ItemsSource绑定来绑定ListBox


请注意,上面的代码不一定是最佳实践。

您的代码中不需要这一行

ListBox lb = new ListBox();
你可以守住那条线

lb.ItemsSource = Products;
或者,您可以将绑定到MobileServiceCollection的内容放在XAML中

<ListBox Margin="10,10,10,100" x:Name="lb" ItemsSource="{Binding Products}">
       <ListBox.ItemTemplate>
         <DataTemplate>
           <TextBlock Text="{Binding Name}"/>
           <TextBlock Text="{Binding Title}"/>
           <TextBlock Text="{Binding Description}"/>
         </DataTemplate>
     </ListBox.ItemTemplate>
</ListBox>

代码隐藏中不需要这一行

ListBox lb = new ListBox();
你可以守住那条线

lb.ItemsSource = Products;
或者,您可以将绑定到MobileServiceCollection的内容放在XAML中

<ListBox Margin="10,10,10,100" x:Name="lb" ItemsSource="{Binding Products}">
       <ListBox.ItemTemplate>
         <DataTemplate>
           <TextBlock Text="{Binding Name}"/>
           <TextBlock Text="{Binding Title}"/>
           <TextBlock Text="{Binding Description}"/>
         </DataTemplate>
     </ListBox.ItemTemplate>
</ListBox>


所以我还在学c,你能举个例子说明你的意思吗。因为我不知道你的意思:“下一步,将产品作为类的属性公开(如果你想使用MVVM,请使用ViewModel),并确保列表框的DataContext设置为所述ViewModel/class。”因此我尝试使用你的代码,但windows universal不支持它,因此我尝试用元素替换它,你现在能看一下我的密码吗。我知道我很烦人,但如果你能帮助我,我会非常感激。@user6480540:啊,是的。我大部分时间都在做WPF,所以是Page。这只是一个例子。对于
local
,您需要将其声明为命名空间。我将把这个例子添加到我的XAML中。所以我开始觉得自己是个十足的白痴,但我用page试过了。我为MainViewModel使用了一个单独的类,因为我不能使用嵌套类。但我现在有两个问题:)。命名空间中不存在第一个仍然是MainViewModel,第二个是所有页面在InitializeComponent中都有错误。如果你没有解决方案,请你给我一个新的方法来使用查询,因为我正在为学校构建这个应用程序,如果我不能让它正常工作,我的整个应用程序都会失败。你可以。压缩你的解决方案并上传到某个地方,或者你可以把它放到github或类似的地方吗?我很乐意看一看,但是在这个论坛上交流有点困难。所以我还在学习c#,你能举个例子吗