我是否缺少某些东西,或者还有更多我没有得到的东西。我正在开发一个移动应用程序,必须使用选择器从数据表中进行选择。首先,我有很多这样的选择器都是基于键/值的。我有一个内部ID和相应的Show值。 ID并不总是具有1、2、3值,例如源自查找表,并且可能具有
KeyID / ShowValue
27 = Another Thing
55 = Many More
12 = Some Item
检索简单至
select * from LookupTable where Category = 'demo'
所以我下面有一个用于通过记录列表绑定选择器的类
public class CboIntKeyValue
{
public int KeyID { get; set; } = 0;
public string ShowValue { get; set; } = "";
}
现在,我要绑定到的数据记录只有与查找关联的ID列。没有陷入xaml中,但是总的来说,我有ViewModel。关于这一点,我有一个数据记录实例,该实例具有ID列。
public class MyViewModel : BindableObject
{
public MyViewModel()
{
// Sample to pre-load list of records from data server of KVP
PickerChoices = GetDataFromServerForDemo( "select * from LookupTable where Category = 'demo'" );
ShowThisRecord = new MyDataRec();
// for grins, I am setting the value that SHOULD be defaulted
// in picker. In this case, ID = 12 = "Some Item" from above
ShowThisRecord.MyID = 12;
}
// this is the record that has the "ID" column I am trying to bind to
public MyDataRec ShowThisRecord {get; set;}
// The picker is bound to this list of possible choices
public List<CboIntKeyValue> PickerChoices {get; set;}
}
我无法绑定到列表的INDEX,因为当我期望相应的“ ID”成为列表中正确记录的基础时,它会给我0、1、2。
在WPF中,我过去曾经能够声明屏幕的SHOW值,但是也可以在ID列中声明BIND VALUE。因此,在我的“ ShowThisRecord”上的INT属性的绑定将驱动并正确刷新。
我可以看到SelectedItem的绑定,但这是KVP类的整个项目,它不属于MyDataRec。它们之间只有共同的元素是ID。
任何人都可以帮助我指导适当的绑定以使其正常工作。
<Picker ItemDisplayBinding="{Binding ShowValue}"
SelectedItem="{Binding ???}" />
只是为了确认我的记录绑定是合法的,我的页面具有MyViewModel的绑定上下文,因为我可以通过添加到页面的示例文本条目正确看到ID。
<Entry Text="{Binding Path=ShowThisRecord.MyID}"/>
谢谢
答案 0 :(得分:1)
我创建了一个演示来测试您的代码,它可以正常工作。完整的演示是here。我还添加了一个功能来验证所选项目。
如果要同步获取SelectedItem
对象,则应实现MyViewModel INotifyPropertyChanged
,并且我为selectedRecord
创建了一个SelectedItem
字段,因此您可以这个:
public class MyViewModel :ViewModelBase
{
public MyViewModel()
{
// Sample to pre-load list of records from data server of KVP
//PickerChoices = GetDataFromServerForDemo("select * from LookupTable where Category = 'demo'");
PickerChoices = new ObservableCollection<TestModel>() {
new TestModel{ MyID = 5,ShowValue="test1" }, new TestModel{ MyID = 9,ShowValue="test2" },
new TestModel{ MyID = 18,ShowValue="test18" }, new TestModel{ MyID = 34,ShowValue="test4" }
};
// set the default selected item
// foreach (TestModel model in PickerChoices) {
// if (model.MyID == 18) {// default value
// SelectedRecord = model;
// break;
// }
// }
ShowThisRecord = new TestModel();
// for grins, I am setting the value that SHOULD be defaulted
// in picker. In this case, ID = 12 = "Some Item" from above
ShowThisRecord.MyID = 12;
}
// this is the record that has the "ID" column I am trying to bind to
public TestModel ShowThisRecord { get; set; }
//*****************************************
TestModel selectedRecord; //selected item object
public TestModel SelectedRecord
{
get { return selectedRecord; }
set
{
if (selectedRecord != value)
{
selectedRecord = value;
OnPropertyChanged();
}
}
}
//*****************************************
// The picker is bound to this list of possible choices
public ObservableCollection<TestModel> PickerChoices { get; set; }
}
ViewModelBase
类
public class ViewModelBase: INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
还有xaml
<Picker Title="Select a value" x:Name="mypicker"
ItemsSource="{Binding Path= PickerChoices}"
SelectedItem="{Binding SelectedRecord}"
ItemDisplayBinding="{Binding MyID}"/>
xaml.cs
public partial class MainPage : ContentPage
{
ObservableCollection<TestModel> items = new ObservableCollection<TestModel>();
MyViewModel testModel = null;
public MainPage()
{
InitializeComponent();
testModel = new MyViewModel();
BindingContext = testModel;
//This will also work
//if (testModel!=null && testModel.PickerChoices!=null) {
// for (int index=0;index< testModel.PickerChoices.Count;index++ ) {
// TestModel temp = testModel.PickerChoices[index];
// if (18 == temp.MyID) {
// mypicker.SelectedIndex = index;
// break;
// }
// }
//}
foreach (TestModel model in testModel.PickerChoices)
{
if (model.MyID == 18)
{// default value
testModel.SelectedRecord = model;
break;
}
}
}
// to show the selected item
private void Button_Clicked(object sender, EventArgs e)
{
if (testModel.SelectedRecord!=null) {
DisplayAlert("Alert", "selected Item MyID : " + testModel.SelectedRecord.MyID + "<--> ShowValue: " + testModel.SelectedRecord.ShowValue, "OK");
}
}
}
结果是:
答案 1 :(得分:0)
您需要将ItemsSource属性设置为CboIntValue项目列表:
<Picker Title="Select a value"
ItemsSource="{Binding PickerChoices}"
ItemDisplayBinding="{Binding ShowValue}" />
答案 2 :(得分:0)
经过大量工作,我最终根据自己的需要编写了自己的独立类和模板样式。尽量做到长度,并发布源代码供任何人使用、审查、评估等等,我将其发布在 CodeProject 上。
https://www.codeproject.com/Articles/5266341/Xamarin-Picker-with-ID-based-Binding
希望这可以帮助其他有类似问题的人。同样,我遇到的主要问题是,如果我有一个来自数据源的整数键 ID,选择器不会仅通过给定的 ID(整数或字符串)自动刷新自身。