本文介绍了Silverlight的组合框和的SelectedItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情形:

1列表,其中包含了一年中的月份:

1 List which contains the months of the year:

public List<String> Months
{
    get
    {
    	return m_Months;
    }
}


m_Months = new List<String>();

for (int i = 1; i <= 12; i++)
{
    DateTime date = new DateTime(1900, i, 1);
    m_Months.Add(date.ToString("MMM"));
}



1组合框的的ItemsSource绑定到月列表,其的SelectedIndex为界的财产一个月,这是一个字符串:

1 ComboBox whose ItemsSource is bound to the Months-list and whose SelectedIndex is bound to the property Month, which is a string:

public string Month
    	{
    		get
    		{
    			return m_Month;
    		}
    		set
    		{
    			if (value != m_Month)
    			{
    				m_Month = value;
    				NotifyPropertyChanged("Month");
    			}
    		}
    	}

<ComboBox SelectedItem="{Binding Month, Mode=TwoWay}" ItemsSource="{Binding Months}" />

当我从代码隐藏,即月=五一设定年,这是正确传播到组合框,并为本月吸气访问,但组合框不显示'五一',因为它的选择项

When I set the Year from the codebehind, i.e. Month = "May", this is properly propagated to the ComboBox, and the getter for Month is accessed, but the ComboBox doesn't show 'May' as it's selected item.

我想知道:是这样的在Silverlight 3中的错误?当我使用Telerik的的radcombobox控件它工作正常。

I'd like to know: is this a bug in Silverlight 3? It works fine when I use the RadComboBox from Telerik.

干杯,
弗朗西丝

Cheers,Frances

推荐答案

感谢您的答复。想你的建议已经得到相同的结果。不过,我刚刚有了一个辛普森时刻(DOH!),并发现我设定的前一个月组合框设置它的ItemsSource。

Thanks for your reply. Tried your suggestion already and get the same results. However, I've just had a Homer Simpson moment (Dôh!) and found out that I set the Month before the ComboBox sets it's ItemsSource.

奇怪,虽然,radcombobox控件正确地做一切。也许它再次检索的SelectedItem当ItemsSource的变化

Strange, though, that the RadComboBox does everything correctly. Maybe it retrieves the SelectedItem again when the ItemsSource changes.

编辑:

好吧,我只是从字面上跌下我的椅子惊奇。显然,SL3仍然有一些错误是需要修复。欣赏一下下面的...

OK, I just literally fell off my chair with amazement. Apparently, SL3 still has some bugs that need fixing. Feast your eyes on the following...

这不工作:

<ComboBox SelectedItem="{Binding Month, Mode=TwoWay}" ItemsSource="{Binding Months}" />

和这样做:

<ComboBox ItemsSource="{Binding Months}" SelectedItem="{Binding Month, Mode=TwoWay}" />

请参阅微小的区别?只要我之前设置的的ItemsSource 在XAML中的SelectedItem,一切都很好,在世界上。 Wowee,我从来不知道XAML呈线性解析!

See the tiny difference? As long as I set the ItemsSource before the SelectedItem in the XAML, all is well in the world. Wowee, I never knew XAML was parsed linearly!

这篇关于Silverlight的组合框和的SelectedItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 11:24