我有一个Popup页面,其中包含一个Picker,它绑定到一个类的项目集合。我无法将SelectedItem属性绑定到绑定上下文的属性,并在SelectedIndexChanged事件处理程序中使用它。我显然犯了一个错误,请帮助我找到它。下面是我的代码。
AbilityScore类:
public class AbilityScore
{
public int Id { get; set; }
public string Name { get; set; } = "";
public string NameLocalized { get; set; } = "";
}
字符串
弹出页面后面的代码:
public partial class PopUpCreationAbilityScoreBoosts : Popup
{
public AbilityScore selectedAbilityScore {get;set;}
public PopUpCreationAbilityScoreBoosts(ILocalizationResourceManager LocalizationResourceManager, Character player)
{
InitializeComponent();
//<some code>
Picker picker = new Picker();
foreach (AbilityScore score in AbilityScores.List)
{
if (staticBoosts.Contains(score))
scores.Remove(score);
}
picker.ItemDisplayBinding = new Binding("NameLocalized");
picker.SetBinding(Picker.SelectedItemProperty, "selectedAbilityScore");
picker.ItemsSource = scores;
picker.SelectedIndexChanged += PickerAncestryFreeBoost_SelectedIndexChanged;
StackAncestryFreeBoosts.Children.Add(picker);
}
private void PickerAncestryFreeBoost_SelectedIndexChanged(object? sender, EventArgs e)
{
Label l = new Label();
l.SetBinding(Label.TextProperty, "selectedAbilityScore.NameLocalized");
StackAncestryFreeBoosts.Children.Add(l);
}
}
型
我尝试进行绑定,以便当我在选择器中更改项目的选择时,添加一个标签,该标签是选定项目的名称属性。项目被正确添加为选择器itemsource,但当我选择一个项目时,添加的标签为空,所以我猜绑定不起作用,它将SelectedItemProperty绑定到selectedAbilityScore属性。
项目已正确添加
的数据
但一个标签是空的
的
1条答案
按热度按时间wfveoks01#
作为回答,根据杰森的评论:
只需使用
myPicker.SelectedItem
来获取对象就可以解决这个问题。