Xamarin.forms 向列表视图中的单选按钮添加命令
我有一个列表视图,它迭代了许多问题,最终用户需要用单选按钮或复选框回答这些问题 视图模型将“问题”指定为:Xamarin.forms 向列表视图中的单选按钮添加命令,xamarin.forms,Xamarin.forms,我有一个列表视图,它迭代了许多问题,最终用户需要用单选按钮或复选框回答这些问题 视图模型将“问题”指定为: TheQuestions=newobservedcollection(); 添加(新问题) { Id=123, 属性1=值1, 问题是什么, 答案=错误, }); 列表视图如下所示: <ListView ItemsSource="{Binding TheQuestions, Mode=TwoWay}"> <ListView.ItemTemplat
TheQuestions=newobservedcollection();
添加(新问题)
{
Id=123,
属性1=值1,
问题是什么,
答案=错误,
});
列表视图如下所示:
<ListView ItemsSource="{Binding TheQuestions, Mode=TwoWay}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding TheQuestion}" />
<StackLayout Orientation="Horizontal">
<RadioButton Command="{ ?? }" CommandParameter="{Binding .}" GroupName="{Binding Id,Mode=TwoWay}" Text="Yes" TextColor="Green" />
<RadioButton Command="{Binding ??}" GroupName="{Binding Id, Mode=TwoWay}" Text="No" TextColor="Red" />
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
稍后,我将为一些问题添加复选框和条目。在用所选答案填充viewmodel时,我遇到了困难。它是可行的还是应该考虑另一种方法?< P>你可以直接将ReloButt的命令绑定到VIEWMODEL,这样就不需要在模型中多次定义它。 在Xaml中
public xxxViewModel()
{
问题=新的可观察集合();
添加(新问题)
{
Id=123,
属性1=值1,
问题是什么,
答案=错误,
});
无线电命令=新命令((obj)=>{
var模型=作为问题的obj;
var answer=模型答案;
//做你想做的事
});
}
您可以将RadioButton的命令直接绑定到ViewModel,这样就不需要在Model中多次定义它
在Xaml中
public xxxViewModel()
{
问题=新的可观察集合();
添加(新问题)
{
Id=123,
属性1=值1,
问题是什么,
答案=错误,
});
无线电命令=新命令((obj)=>{
var模型=作为问题的obj;
var answer=模型答案;
//做你想做的事
});
}
你为什么不把IsChecked绑定到你的虚拟机上?好问题,我陷入了兔子洞,没有想到这么简单的解决方案。你为什么不把IsChecked绑定到你的虚拟机上?好问题,我陷入了兔子洞,没有想到这么简单的解决方案。
<ListView ItemsSource="{Binding TheQuestions, Mode=TwoWay}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding TheQuestion}" />
<StackLayout Orientation="Horizontal">
<RadioButton Command="{ ?? }" CommandParameter="{Binding .}" GroupName="{Binding Id,Mode=TwoWay}" Text="Yes" TextColor="Green" />
<RadioButton Command="{Binding ??}" GroupName="{Binding Id, Mode=TwoWay}" Text="No" TextColor="Red" />
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<?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:App32"
x:Class="App32.MainPage"
x:Name="Page" // set name of ContentPage
>
<RadioButton Command="{Binding Source={x:Reference Page}, Path=BindingContext.RadioCommand}" CommandParameter="{Binding .}" GroupName="{Binding Id,Mode=TwoWay}" Text="Yes" TextColor="Green" />
<RadioButton Command="{Binding Source={x:Reference Page}, Path=BindingContext.RadioCommand}" CommandParameter="{Binding .}" GroupName="{Binding Id, Mode=TwoWay}" Text="No" TextColor="Red" />
public ICommand RadioCommand { get; set; }
public xxxViewModel()
{
TheQuestions = new ObservableCollection<Question>();
TheQuestions.Add(new Question
{
Id = 123,
Attribute1 = Value1,
TheQuestion = "Blah blah?",
Answer = false,
});
RadioCommand = new Command((obj)=> {
var model = obj as Question;
var answer = model.Answer;
//do something you want
});
}