我发现this的GitHub帖子显示了一个方便的Xamarin DataGrid。但是,我想更进一步,并在最左边的列中添加一个复选框,以便我可以在按钮上单击以捕获已选择的网格中的所有ID。
这在Xamarin.Forms
和C#
中可以实现吗?
修改
因此,经过大量的搜索之后,我发现使用“切换”会容易得多,并且我的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行。我要用正确的方法吗?如何绑定数据?
答案 0 :(得分:0)
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
contentPage.BindingContext= new MainViewModel()
答案 1 :(得分:0)
您的源代码中没有要绑定的列表。首先,您需要它。 这是Xaml的DataGrid实现,没有任何块或第三方应用程序。 https://www.youtube.com/watch?v=IPIyzWpkrHU