Xamarin DataGrid如何绑定? [英] Xamarin DataGrid How To Bind?

查看:62
本文介绍了Xamarin DataGrid如何绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现 GitHub帖子,其中显示了方便的Xamarin DataGrid.但是,我想更进一步,并在最左边的列中添加一个复选框,这样我就可以在按钮上单击以捕获已选择的网格中的所有ID.

I found this GitHub post that shows a handy Xamarin DataGrid. However, I want to take it a step further and add a checkbox as the left most column, so that I can then on button click capture all the ID's from the grid that have been selected.

Xamarin.Forms C#中可以实现吗?

修改
因此,经过大量的搜索之后,我发现使用切换"会容易得多,并且我的XAML拥有此代码.我的问题是如何将数据库字段绑定到标签的绑定?

edit
So after much googling I discovered it would be much easier to use a "toggle" and I have this code for my XAML. My issue is how do I bind my database fields to the binding for the labels?

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
         x:Class="Test.Pages.TestApprove" >
<ContentPage.Content>
<StackLayout>
<Label Text="The users below are Requesting Access:"></Label>
<Grid Padding="5,0" RowSpacing="1" ColumnSpacing="1">
    <Grid.RowDefinitions>
        <RowDefinition Height="auto" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="auto" />
    </Grid.ColumnDefinitions>
    <Switch Grid.Row="0" Grid.Column="0" VerticalOptions="Center" 
            HorizontalOptions="Center" BackgroundColor="Brown" />
    <Label Text="{Binding fname}" Grid.Row="0" Grid.Column="1" 
           Grid.ColumnSpan="1" Margin="1" 
           BackgroundColor="Blue" IsEnabled="false"/>
    <Entry Text="{Binding lname}" Grid.Row="0" Grid.Column="2" 
           Grid.ColumnSpan="1" Margin="1" IsEnabled="false"
           FontSize="Small" BackgroundColor="Purple" />
    <Entry Text="{Binding company}" Grid.Row="0" Grid.Column="3" 
           Grid.ColumnSpan="1" Margin="1"
           FontSize="Small" BackgroundColor="Green" />
    <Entry Text="{Binding Phone}" Grid.Row="0" Grid.Column="4" 
           Grid.ColumnSpan="1" Margin="1"  
           FontSize="Small" BackgroundColor="Orange" />
</Grid>
<Button Command="{Binding ApproveUserCommand}" Text="Approve User" TextColor="White"
        FontAttributes="Bold" FontSize="Large" HorizontalOptions="FillAndExpand"  
        BackgroundColor="#088da5" />
</StackLayout>
</ContentPage.Content>  
</ContentPage>

当然,我想动态生成选择查询返回的行数...因此,如果有10个请求访问的用户,则每个用户数据应为10行.我要这样做吗?如何绑定数据?

And of course I want to dynamically generate the number of rows that the select query returns...so if there are 10 users requesting access, I should be 10 rows with each users data. Am I going about this the correct way/. How do I bind the data?

推荐答案

Xaml:

<ContentPage x:Name="self">
  <dg:DataGrid ItemsSource="{Binding Users}">
    <dg:DataGrid.Columns>
          <dg:DataGridColumn Title="Authorized" PropertyName="IsAuthorized" Width="100">
            <dg:DataGridColumn.CellTemplate>
              <DataTemplate>
                <Switch IsToggled="{Binding .}" HorizontalOptions="Center" VerticalOptions="Center"/>
              </DataTemplate>
            </dg:DataGridColumn.CellTemplate>
          </dg:DataGridColumn>
          <dg:DataGridColumn Title="Name" PropertyName="Name" Width="2*"/>
          <dg:DataGridColumn Title="Action" PropertyName="Id" PropertyName="Streak" Width="0.7*" IsSortable="False">
            <dg:DataGridColumn.CellTemplate>
              <DataTemplate>
                <!-- Here is the trick: command binds the ViewModel's method, since we needed the Id, we've added the id as column's binding context -->
                <Button Text="Authorize" Command="{Binding AuthorizeCommand,source={x:Reference self}}" CommandParameter="{Binding .}"/>
              </DataTemplate>
            </dg:DataGridColumn.CellTemplate>
          </dg:DataGridColumn>
    </dg:DataGrid.Columns>
  </dg:DataGrid>
</ContentPage>

ViewModel:

class MainViewModel
{

  public MainViewModel()
  {
    AuthorizeCommand = new Command<string>(CmdAuthorize);
  }

  public ICommand AuthorizeCommand{get;set;}
  public List<User> Users{get;set;}

  CmdAuthorize(string id)
  {
    var user = Users.First(x=>x.Id == id);
    user.IsAuthorized = true;
  }
}

型号:

class User: INotifyPropertyChanged
{
  bool _isAuthorized;
  public event PropertyChangedEventHandler PropertyChanged;
  public string Name{get;set;}  
  public string Id{get;set;}
  public bool IsAuthorized
  {
    get=>_isAuthorized;
    set
    { 
      _isAuthorized =value;
       PropertyChanged?.Invoke(nameof(IsAuthorized));
    }
  }
}

ps:不要忘记设置页面的 BindingContext

ps: Do not forget setting BindingContext of the page

contentPage.BindingContext= new MainViewModel()

这篇关于Xamarin DataGrid如何绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆