[英]Combobox not binding initial value when page first shown
The Scenario 场景
I have a UserControl
which contains a couple of controls, one of which is a ComboBox
. 我有一个
UserControl
,其中包含几个控件,其中一个是ComboBox
。 The UserControl
is conatined with a Page
. UserControl
带有Page
。
The UserControl
exposes a couple of dependency properties to allow the Items
and SelectedValue
of the ComboBox
to be accessed. UserControl
公开了几个依赖项属性,以允许访问ComboBox
的Items
和SelectedValue
。 These are defined as follows: 这些定义如下:
public static readonly DependencyProperty SelectedValueProperty = DependencyProperty.Register("SelectedValue", typeof(int?), typeof(CustomComboBox), new FrameworkPropertyMetadata((int?)null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public int? SelectedValue
{
get { return (int?)this.GetValue(SelectedValueProperty); }
set { this.SetValue(SelectedValueProperty, value); }
}
public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register("Items", typeof(List<ComboItem>), typeof(CustomComboBox), new FrameworkPropertyMetadata((List<ComboItem>)new List<ComboItem>(), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public List<ComboItem> Items
{
get { return (List<ComboItem>)this.GetValue(ItemsProperty); }
set { this.SetValue(ItemsProperty, value); }
}
These are binded to the ComboBox
control as follows: 它们被绑定到
ComboBox
控件,如下所示:
<ComboBox ItemsSource="{Binding Path=Items, ElementName=ThisControl}" DisplayMemberPath="Text" SelectedValuePath="ID" SelectedValue="{Binding Path=SelectedValue, ElementName=ThisControl, UpdateSourceTrigger=PropertyChanged}" />
Where the UserControl
is named "ThisControl"
. 其中
UserControl
命名为"ThisControl"
。
Also, in the above, ComboItem
is a class which contains the definitions for each item, namely two properties called ID
and Text
. 同样,在上面,
ComboItem
是一个类,其中包含每个项目的定义,即两个名为ID
和Text
属性。 The idea here is that ID
is the key that is stored in the database, and Text
is the user friendly value that is displayed in the list. 这里的想法是
ID
是存储在数据库中的密钥,而Text
是显示在列表中的用户友好值。
When the Page
containing the UserControl
is created (using the Constructor), the ComboBox items list is populated with the following code: 创建包含
UserControl
的Page
(使用构造函数)后,将使用以下代码填充ComboBox项列表:
MyComboBox.Items.Clear();
MyComboBox.Items.Add(new ComboItem() { ID = 1, Text = "One" });
MyComboBox.Items.Add(new ComboItem() { ID = 2, Text = "Two" });
Finally, the SelectedValue
is binded to a class property via XML like so: 最后,
SelectedValue
通过XML绑定到类属性,如下所示:
<c:CustomComboBox x:Name="MyComboBox" SelectedValue="{Binding Path=MyClassInstance.ItemID}" />
where the property ItemID
is of type int
. 其中属性
ItemID
的类型为int
。 It is important to note that the class instance is set after the combo box items are populated. 重要的是要注意,在填充组合框项目之后设置了类实例。
The Problem 问题
When the page is first shown, the initial selected value is not set correctly - it is an empty item selection. 首次显示该页面时,初始选择的值设置不正确-这是一个空项目选择。 I can assure that the
ItemID
property is already set to a valid value (ie 1
). 我可以确保
ItemID
属性已经设置为有效值(即1
)。
When an item is selected from the ComboBox
, the binding does successfully set the ItemID
property, and is correctly persisted to the database upon a 'save' function. 当从
ComboBox
选择一个项目时,绑定确实会成功设置ItemID
属性,并通过“保存”功能正确地保留在数据库中。
One strange behavior, which may help to identity the problem, is that if the window containing the Page
is closed, and then re-opened. 一种可能有助于识别问题的奇怪行为是,如果包含
Page
的窗口被关闭,然后重新打开。 The ComboBox
does get th correct initial value - and that is regardless of the ComboBox being set on first attempt, or otherwise. ComboBox
确实获得了正确的初始值-这与在初次尝试时是否设置ComboBox或其他方式无关。
So, the question is: Why is the initial value not shown the first time the page is displayed? 因此,问题是:为什么初次显示页面时不显示初始值?
OK, I was doing some refactoring to improve how I populate the ComboBox Items
and I seem to have stumbled across the solution... 好的,我正在做一些重构以改善填充ComboBox
Items
而且我似乎偶然发现了该解决方案...
The problem seems to be related to the following piece of code that populates the list items: 该问题似乎与填充列表项的以下代码有关:
MyComboBox.Items.Clear();
MyComboBox.Items.Add(new ComboItem() { ID = 1, Text = "One" });
MyComboBox.Items.Add(new ComboItem() { ID = 2, Text = "Two" });
The solution is to directly set the Items
property, rather than to clear it and populate it: 解决方案是直接设置
Items
属性,而不是清除并填充它:
MyComboBox.Items = GetMyList();
Where GetMyList()
is returning a List<ComboItem>
type. 其中
GetMyList()
返回List<ComboItem>
类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.