为了说明成功和失败的情况,我要使用以下后端。在每种情况下,我都有一组事物,以及一个属性,该属性设置为array.First()。
此外,一旦我打开
所以我的问题是,为什么?有什么不同导致这种情况发生?
public class MainPage
{
public MainPage()
{
this.InitializeComponent();
FirstString = Strings.First();
FirstItem = Items.First();
}
public string FirstString { get; set; }
public Item FirstItem { get; set; }
public string[] Strings => new[] {"1", "2", "3", "4"};
public Item[] Items => new[]
{
new Item {Index = 1},
new Item {Index = 2},
new Item {Index = 3},
new Item {Index = 4}
};
}
public class Item
{
public int Index { get; set; }
}
因此,绑定结果在SelectedItem
中的项目被选择。
<ComboBox ItemsSource="{x:Bind Strings}"
SelectedItem="{x:Bind FirstString}">
<ComboBox.ItemTemplate>
<DataTemplate x:DataType="system:String">
<TextBlock Text="{x:Bind}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
以下是未能选择备用项目的相同代码
<ComboBox ItemsSource="{x:Bind Items}"
SelectedItem="{x:Bind FirstItem}">
<ComboBox.ItemTemplate>
<DataTemplate x:DataType="local:Item">
<TextBlock Text="{x:Bind Index}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
此外,一旦我打开
Mode=TwoWay
,这将在初始绑定期间清除我的VM中的值。所以我的问题是,为什么?有什么不同导致这种情况发生?