【问题标题】:Xamarin Forms Bindable GridXamarin 窗体可绑定网格
【发布时间】:2017-01-27 12:20:18
【问题描述】:

我正在开发 Xamarin Forms 应用程序,需要能够绑定到 Grid。我偶然发现了这篇文章:

https://www.linkedin.com/pulse/bindable-grid-xamarin-forms-jonas-frid

GitHub 上的来源:

https://github.com/Manne990/XamTest/blob/master/XamTest/Views/TemplatedTableView/TemplatedTableView.cs

这正是我所需要的,但它不会响应集合中的变化。我正在使用 MvvmHelpers,它适用于所有 ListView:

https://github.com/jamesmontemagno/mvvm-helpers

我假设我需要更新 TemplatedTableView.cs 以响应 CollectionChanged 事件 - 我只是不确定如何执行此操作?

【问题讨论】:

标签: c# mvvm data-binding xamarin.forms


【解决方案1】:

在 Xamarin Forms 3.5 中,您可以使用 Bindable Layouts

来自官方文档的示例:

<StackLayout BindableLayout.ItemsSource="{Binding User.TopFollowers}"
             Orientation="Horizontal"
             ...>
    <BindableLayout.ItemTemplate>
        <DataTemplate>
            <controls:CircleImage Source="{Binding}"
                                  Aspect="AspectFill"
                                  WidthRequest="44"
                                  HeightRequest="44"
                                  ... />
        </DataTemplate>
    </BindableLayout.ItemTemplate>
</StackLayout>

【讨论】:

    【解决方案2】:

    你可以试试Syncfusion提供的网格

    在 XAML 中使用

    <?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:local="clr-namespace:DataGridDemo;assembly=DataGridDemo"
                 xmlns:syncfusion="clr-namespace:Syncfusion.SfDataGrid.XForms;assembly=Syncfusion.SfDataGrid.XForms" 
                 x:Class="DataGridDemo.Sample">
    
        <ContentPage.BindingContext>
            <local:OrderInfoRepository x:Name="viewModel" />
        </ContentPage.BindingContext>
    
        <ContentPage.Content>
            <syncfusion:SfDataGrid x:Name="dataGrid"
                                   ItemsSource="{Binding OrderInfoCollection}">
            </syncfusion:SfDataGrid>
        </ContentPage.Content>
    </ContentPage>
    

    一定要在绑定集合更新后调用属性改变事件

    它有很多简洁的功能。

    【讨论】:

    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
    • 还添加了 sn-p 和图像
    • 请注意,这是一个商业产品,因此任何为他们的晚间项目寻找解决方案的人都可以查看 FlowListView 或寻找其他地方。
    【解决方案3】:

    也许会有所帮助:https://github.com/daniel-luberda/DLToolkit.Forms.Controls/tree/master/FlowListView - 它支持项目更新/分组等等

    示例代码:

    <flv:FlowListView x:Name="flowListView" FlowColumnCount="3" SeparatorVisibility="None" HasUnevenRows="false"
        FlowItemTappedCommand="{Binding ItemTappedCommand}" FlowLastTappedItem="{Binding LastTappedItem}"
        FlowItemsSource="{Binding Items}" >
    
        <flv:FlowListView.FlowColumnTemplate>
            <DataTemplate>
                <Label HorizontalOptions="Fill" VerticalOptions="Fill" 
                    XAlign="Center" YAlign="Center" Text="{Binding Title}"/>
            </DataTemplate>
        </flv:FlowListView.FlowColumnTemplate>
    
    </flv:FlowListView> 
    

    【讨论】:

      【解决方案4】:

      您可以将事件添加到ItemSource 属性的CollectionChanged 事件。

      这就是 Xamarin Forms 中 ListView 的实现方式。您可以在github 中找到相同的代码。

      例如:

          void OnItemsSourceChanged(bool fromGrouping = false)
          {
              ListProxy.CollectionChanged -= OnProxyCollectionChanged;
      
              IEnumerable itemSource = GetItemsViewSource();
              if (itemSource == null)
                  ListProxy = new ListProxy(new object[0]);
              else
                  ListProxy = new ListProxy(itemSource);
      
              ListProxy.CollectionChanged += OnProxyCollectionChanged;
              OnProxyCollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
          }
      

      【讨论】:

      • 感谢您的回复 - 我无法让它工作。我确定这是由于我缺乏理解 - 你有更全面的例子吗?同时,我遇到了gist.github.com/NVentimiglia/2723411428cdbb72fac6,并且已经能够得到我想要的结果
      • 很抱歉,我现在只能为您提供示例和伪代码。您提供的链接几乎相同,但通过 ObservableCollection。它有一些开销,但我认为你应该没问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-25
      • 1970-01-01
      • 1970-01-01
      • 2019-12-01
      • 2017-10-05
      • 2020-06-18
      • 1970-01-01
      相关资源
      最近更新 更多