【问题标题】:Xamarin DataGrid How To Bind?Xamarin DataGrid 如何绑定?
【发布时间】:2018-11-26 21:52:52
【问题描述】:

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

这在Xamarin.FormsC# 中可以实现吗?

编辑
因此,经过大量谷歌搜索后,我发现使用“切换”会更容易,并且我的 XAML 有此代码。我的问题是如何将我的数据库字段绑定到标签的绑定?

<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 行。我是否以正确的方式解决这个问题/。如何绑定数据?

【问题讨论】:

  • 可以实现吗?当然。但是目前还没有内置的复选框控件,因此您必须自己构建/查找(那里有很多示例)。
  • @Jason - 感谢您的建议。我的意思是可以实现我的结果的内置或多合一控件。我会继续进行谷歌搜索,因为我所发现的一切都“完全”是我所追求的:D
  • 或者使用像 Syncfusion 等商业网格
  • @Jason - 我是否在正确的方向上使用我链接到的网格并使用我发现的多个复选框之一?还是这样不妥?
  • 只要支持在单元格内使用任意控件,我都会这么认为

标签: c# xamarin xamarin.forms datagrid xamarin.ios


【解决方案1】:

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>

视图模型:

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

contentPage.BindingContext= new MainViewModel()

【讨论】:

    【解决方案2】:

    在您的源代码中没有要绑定的列表。首先你需要它.. 这是一个带有 Xaml 的 DataGrid 实现,没有任何块或第三方应用程序。 https://www.youtube.com/watch?v=IPIyzWpkrHU

    【讨论】:

    • 为了立即对读者有所帮助(并避免Linkrot),我们更喜欢至少直接提供解决方案的可靠摘要的答案,并带有用于提供其他信息的链接。 More info on link only answers.
    猜你喜欢
    • 1970-01-01
    • 2017-08-05
    • 2014-09-12
    • 2011-10-12
    • 2014-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多