首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xamarin形成Picker结合

Xamarin形成Picker结合
EN

Stack Overflow用户
提问于 2017-08-23 21:53:14
回答 3查看 37.4K关注 0票数 7

我想实现一个简单的选择器XAML绑定到3标签,当我从选择器中选择一个值时,标签将自动填充(数据来自SQLite)。以下是我所拥有的:

代码语言:javascript
复制
 <Picker x:Name="ListJobs" Title="Select Job"  ItemsSource="{Binding options}" ItemDisplayBinding="{Binding JobNo}" SelectedItem="{Binding SelectedJobs}"/>

 <Label Text="{Binding JobsId}" IsVisible="True" x:Name="TxtId"/>
 <Label Text="{Binding name}" IsVisible="True" x:Name="TxtName"/>
 <Label Text="{Binding location}" IsVisible="True" x:Name="TxtLoc"/>

模型

代码语言:javascript
复制
public class Jobs
{
public string JobsId {get;set;}
public string name {get;set;}
public string location {get;set;}

public Jobs(){}
}

代码背后:

代码语言:javascript
复制
protected override OnAppearing()
{
 jobsInfo = (List<Jobs>) GetJob();
foreach (var item in jobsInfo)
{
  Jobs options = new Jobs
{
  JobsId = item.JobsId,
  name = item.name,
  location = item.location
};
BindingContext = options;
}
}
 private IEnumerable<Jobs> GetJobsInfo()
        {
            var db = _connection.Table<Jobs>();
            return db.ToList();
        }

我会从选择器(如下拉列表)中选择并填充标签。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-08-24 06:21:28

首先,代码中有一些错误。

1.在遍历循环(从db获得的数据)时,选项总是用新数据更新(因此它使用最后一个对象生成),并将其设置为BindingContext。您应该在这里设置一个modelView,而不是一个模型。

2.选择器的itemSource必须是一个list,但是您在这里设置了一个模型。

3.视图模型必须实现INotifyPropertyChanged来通知更改。

我认为你最需要理解的不是这个选择,而是如何使用绑定。

可弯曲性质

数据绑定基础

从数据绑定到MVVM

好吧,让我们回到这个案子。你需要的是这里

我简化了演示,您可以参考它。

  • XAML
  • 模型和ViewModel: 公共类作业{公共字符串JobsId { get;set;}公共字符串名称{ get;set;}公共字符串位置{ get;set;}公共类RootModel : INotifyPropertyChanged { List jobList;公共列表jobList { get {返回jobList;} set { if (jobList =值){ JobList =值;OnPropertyChanged();}}就业selectedJob;公共作业SelectedJob { get {返回selectedJob;} set { if (selectedJob = value) { selectedJob = value;OnPropertyChanged();}公共事件PropertyChangedEventHandler PropertyChanged;受保护的虚拟空OnPropertyChanged(CallerMemberName string propertyName = null) { PropertyChangedEventHandler处理程序= PropertyChanged;if (处理程序!=空){处理程序(此,新的PropertyChangedEventArgs(propertyName));}}
  • 代码背后: 公共MainPage() { InitializeComponent();this.BindingContext =新RootModel { JobList = GetJobsInfo() };}私有列表GetJobsInfo() { var db = _connection.Table();返回db.ToList();}
  • 我的测试:

票数 27
EN

Stack Overflow用户

发布于 2017-08-23 22:09:00

XAML:

代码语言:javascript
复制
<Picker x:Name="ListJobs" Title="Select Job" ItemsSource="{Binding AllJobs}"
        ItemDisplayBinding="{Binding Name}" SelectedItem="{Binding SelectedJob}"/>
<Label Text="{Binding SelectedJob.JobId}" IsVisible="True" x:Name="TxtId"/>
<Label Text="{Binding SelectedJob.Name}" IsVisible="True" x:Name="TxtName"/>
<Label Text="{Binding SelectedJob.Location}" IsVisible="True" x:Name="TxtLoc"/>

型号:

代码语言:javascript
复制
public class Job
{
    public string JobId { get; set; }
    public string Name { get; set; }
    public string Location {get; set; }
}

public class JobModel
{
    public List<Job> AllJobs { get; set; }
    public Job SelectedJob { get; set; }
}

代码背后:

代码语言:javascript
复制
protected override OnAppearing()
{
    BindingContext = new JobsModel {
        AllJobs = GetJobs()
    };
}

private List<Jobs> GetJobs()
{
    var db = _connection.Table<Jobs>();
    return db.ToList();
}
票数 6
EN

Stack Overflow用户

发布于 2022-06-03 06:25:41

代码语言:javascript
复制
         //How to add picker or dropdownlist in Xamarin forms and bind text ?
    
    
    //we will be using Picker Properties -> pickername.ItemsSource and pickername.SelectedItem
    //In above line pickername is the x:Name given to picker 
    
    
    //page.xaml 
    
               <Frame CornerRadius="5" HeightRequest="50"  Padding="0">
                    <StackLayout Orientation="Horizontal">
                        <controls:BorderlessPicker
                                                x:Name="Pickdoctype" 
                                                ItemDisplayBinding="{Binding text}"
                                                SelectedIndexChanged="Pickdoctype_SelectedIndexChanged"
                                                HorizontalOptions="FillAndExpand"                                                                     
                                                Title="Enter Document Type"
                                                FontSize="20" 
                                                TextColor="Gray">
                        </controls:BorderlessPicker>
                        <ImageButton BackgroundColor="Transparent" Padding="0">
                            <ImageButton.Source>
                                <FontImageSource Glyph="&#xf0d7;" Size="25" 
                                                          FontFamily="{StaticResource FontAwesomeSolid}"
                                                          Color="Gray"></FontImageSource>
                            </ImageButton.Source>
                        </ImageButton>
                    </StackLayout>
                </Frame>
                
                
                
     //page.xaml.cs


      
            //Picker Get API
            var responseUserData = await httpService.Get("manageDocument/documentType/documentType/" + UserID);
            if (responseUserData.IsSuccessStatusCode)
            {
                var responseUserDataString = await responseUserData.Content.ReadAsStringAsync();
                var myDeserializedClass = JsonConvert.DeserializeObject<List<DocumentTypeModel>>(responseUserDataString);
                Pickdoctype.ItemsSource = myDeserializedClass;
               
            }
              
              
              
            //Get Picker value functionality
            private void Pickdoctype_SelectedIndexChanged(object sender, EventArgs e)
            {
                try
                {
                    DocumentTypeModel selectedItem = (DocumentTypeModel)Pickdoctype.SelectedItem;
                    string PickerValue = selectedItem.value;
                    string PickerText = selectedItem.text;
                }
                catch (Exception ex)
                {

                }
            }
    
    
    //Create a model class 
    
         public class DocumentTypeModel
        {
            public string masterName { get; set; }
            public string text { get; set; }
            public string value { get; set; }
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45849856

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档