首页 文章

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

提问于
浏览
0

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

[![在此输入图片说明] [1]] [1]

class ProductTable
    {
        public string id { get; set; }
        public string Name { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public double Price { get; set; }
        public string Type { get; set; }
        public string Seller { get; set; }
        public int Expiration { get; set; }
    }

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;
        }
    }
}

XAML

<Page x:Name="PAGE"
xmlns:local="clr-namespace:Test;assembly=Version1">
        <Grid>
            <Grid.DataContext>
                <local:MainViewModel />
            </Grid.DataContext>
            <ListBox Margin="10,10,10,100" x:Name="lb" ItemsSource="{Binding Products}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <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>
        </Grid>
    </Page>

Solution

表需要显式声明JSON属性名,否则您将无法使用数据绑定 .

Table.cs

public class ProductTable
{
    [JsonProperty(PropertyName = "id")]
    public string id { get; set; }

    [JsonProperty(PropertyName = "Name")]
    public string Name { get; set; }

    [JsonProperty(PropertyName = "Title")]
    public string Title { get; set; }

    [JsonProperty(PropertyName = "Description")]
    public string Description { get; set; }

    [JsonProperty(PropertyName = "Price")]
    public double Price { get; set; }

    [JsonProperty(PropertyName = "Type")]
    public string Type { get; set; }

    [JsonProperty(PropertyName = "Seller")]
    public string Seller { get; set; }

    [JsonProperty(PropertyName = "Expiration")]
    public int Expiration { get; set; }
}

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;

}

XAML

IN THE STACKPANEL, NEVER EVER USE HEIGHT"*" this will cause a critical error!

<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>

2 回答

  • 2

    您正在创建两个ListBox - 一个在未显示的代码中,另一个在XAML中,它绑定到您的DataContext .

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

    ItemsSource="{Binding Products}"
    

    接下来,将Products作为属性暴露给类(如果要使用MVVM,则为ViewModel),并确保将ListBox的DataContext设置为所述ViewModel /类 .

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

    Elaboration on Properties

    创建一个类:

    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
        }
    }
    

    在你的XAML中:

    <Page [...]
        xmlns:local="clr-namespace:YourNamespace;assembly=YourProjectName">
        <Grid>
            <Grid.DataContext>
                <local:MainViewModel />
            </Grid.DataContext>
            <!-- ListBox goes in here somewhere, still with ItemsSource bound to Products -->
        </Grid>
    </Page>
    

    阅读有关命名空间here的更多信息 .

    通过这样做,您的Windows'DataContext将被设置为MainViewModel(也可以命名为ProductsViewModel) . 由于您的ListBox将在您的Window中,它将继承(由于Property Value Inheritance)相同的DataContext .

    Binding 将在ViewModel上查找属性'Products' .

    您需要在某处调用 Load() 方法 .

    Part 2 - After looking at the code

    请注意,我无法运行代码,所以我有点失明 .

    Buy.xaml.cs

    public sealed partial class Buy : Page
    {
        private readonly MainViewModel _viewModel;
    
        public Buy()
        {
            this.InitializeComponent();
    
            _viewModel = new MainViewModel();
            DataContext = _viewModel;
        }
    
        private async void button1_Click(object sender, RoutedEventArgs e)
        {
            _viewModel.Load();
        }
    }
    

    MainViewModel.cs

    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)));
        }
    }
    

    您需要公开ProductTable,否则您将收到编译错误 .

    希望上面的内容能够使用上面描述的ItemsSource绑定绑定ListBox .

    请注意,上述代码不一定是最佳实践 .

  • 0

    你的代码中不需要那一行

    ListBox lb = new ListBox();
    

    你可以保持这条线

    lb.ItemsSource = Products;
    

    或者您可以将绑定绑定到XAML中的MobileServiceCollection

    <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>
    

相关问题