我正在尝试解决 .NET MAUI 中的绑定问题。
如截图1所示,我有学生证信息。第一个字段是标签,然后他们转到三个条目。这个想法是当用户点击按钮时,这 3 个条目应该是可读的,编辑按钮应该被替换为提交按钮。
在这种情况下,我需要将父项(堆栈布局)传递给我的 EditButton 事件函数。
XAML 页面的切片如下所示:
<Grid ColumnDefinitions="3*,1*" BackgroundColor="{StaticResource cardbg}" RowSpacing="3">
<StackLayout Grid.Row="0" Grid.Column="0" x:Name="StudentStack"
Margin="20">
<Label x:Name ="idlab" Text="{Binding StudentId, StringFormat='Id={0:D0}'}" TextColor=" {StaticResource cardtextbg}"/>
<Entry Text="{Binding StudentName}" TextColor="{StaticResource cardtextbg}" IsReadOnly="True"/>
<Entry Text="{Binding StudentSurname}" TextColor="{StaticResource cardtextbg}" IsReadOnly="True"/>
<Entry Text="{Binding StudentGPA}" TextColor="{StaticResource cardtextbg}"
IsReadOnly="True"
Margin="10"/>
</StackLayout>
<ImageButton x:Name="edit" Grid.Row="0" Grid.Column="1"
Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodels:StudentsViewModel}}, Path=EditStudents}"
CommandParameter="{Binding Source={RelativeSource AncestorType={x:Type models:Student}}}"
HeightRequest="{Binding Height, Source={x:Reference idlab}}"
Source="edit.svg"
BackgroundColor="CornflowerBlue">
</ImageButton>
</Grid>
所以,卡片是一个 2 列 1 行的网格,里面有一个堆栈布局,卡片右侧有 ImageButton。
我厌倦了以下代码来绑定我的 ImageView 以传递父级。
XAML:
Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodels:StudentsViewModel}}, Path=EditStudents}"
CommandParameter="{Binding Source={RelativeSource AncestorType={x:Type models:Student}}}"
学生视图模型:
public ICommand EditStudents { get; set; }
public StudentsViewModel()
{
EditStudents = new Command(onEditStudents);
}
private void onEditStudents(object obj)
{
var b = 3 + 4;
}
该对象表示 Student 模型,但它不是 StackLayout 的对象,因此我无权在单击 EditButton 时打开/关闭特定卡片的条目。
那是我的问题。
同样,由于打开/关闭 3 个条目的可读属性,我如何将我的 XAML 页面绑定到 ViewModel,并传递 StackLayout(特定卡片的父级)?
功能要怎么样?
喜欢你已经提到的问题下面的评论。
将您的 CommandParameter 绑定到您的实际 StudentModel,然后更改将影响视觉行为的属性