在组合框中选择一个值 [英] Selecting a value in combobox

查看:61
本文介绍了在组合框中选择一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这样的WPF中填充一个组合框



_cviewPreferencesPresenter.ListOfOVUnits();

 cmbOVUnits.SelectedItem = _cstrSelectdOVUnit; 
_cstrDisplayedOVUnitID = cmbOVUnits.SelectedValue.ToString();





组合框加载后我想看到所选的默认值但是我没有得到任何值cmbOVUnits.SelectedItem是null。但是组合框正在填充。



当我在组合框中选择一些值并单击应用按钮时该值与...相关它是第一个价值。



  public  < span class =code-keyword> void  PopulateOVList(List< KeyValuePair< string,string>> lstOVData)
{
try
{
cmbOVUnits.DataContext = lstOVData;
cmbOVUnits.DisplayMemberPath = Value;
cmbOVUnits.SelectedValuePath = Key;
}
catch (FlukeException)
{
throw ;
}
catch (异常excException)
{
如果(FlukeConstants.IsLoggerEnabled)
{
Common.LogApplicationExceptionMsg(excException);
}
throw new FlukeException(SeverityLevel.Fatal,LogLevel.Debug,excException ,
FlukeExceptionConstants.FLK_ERR_COMMON_APPLICATIONEXCEPTION);
}
}





这里我可以看到cmbOVUnits cmbOVUnits = {System.Windows.Controls .ComboBox Items.Count:0}是'0',即使我在lstOVData中获取值.DataContext的值如cmbOVUnits.DataContext = Count = 14.



显然cstrDisplayedOVUnitID值将为null。我如何解决这个问题?

解决方案

您的第一个主要问题是:您使用数据库处理数据。填充语义数据,而不仅仅是字符串。如果您以任何方式使用此类控件中的数据,则item对象最好不是字符串。



组合框的列表项可以是任何对象。另一个问题是:项目无法选择。例如,您可以检查 SelectedIndex 。如果它返回值< 0,没有选择任何内容。您可以使用此属性以编程方式选择项目。它更可靠,因为,例如,从查看你的代码,我不能确定 _cstrSelectdOVUnit 是一个有效的选择对象,因为我不知道你是否添加这个对象是丢失与否。



请仔细阅读并阅读:^]。



-SA

I am populating a combobox in WPF like this

_cviewPreferencesPresenter.ListOfOVUnits();

cmbOVUnits.SelectedItem = _cstrSelectdOVUnit;
            _cstrDisplayedOVUnitID = cmbOVUnits.SelectedValue.ToString();



After the combobox loading i want to see the default value selected but i am not getting any value cmbOVUnits.SelectedItem is null.But the combobox is getting populated.

When i select some value in the combobox and click on apply button the value is not pertaining it is going to first value.

public void PopulateOVList(List<KeyValuePair<string, string>> lstOVData)
    {
        try
        {
            cmbOVUnits.DataContext = lstOVData;
            cmbOVUnits.DisplayMemberPath = "Value";
            cmbOVUnits.SelectedValuePath = "Key";
        }
        catch (FlukeException)
        {
            throw;
        }
        catch (Exception excException)
        {
            if (FlukeConstants.IsLoggerEnabled)
            {
                Common.LogApplicationExceptionMsg(excException);
            }
            throw new FlukeException(SeverityLevel.Fatal, LogLevel.Debug, excException,
                    FlukeExceptionConstants.FLK_ERR_COMMON_APPLICATIONEXCEPTION);
        }
    }



here i can see cmbOVUnits cmbOVUnits = {System.Windows.Controls.ComboBox Items.Count:0} is '0', even i though i am getting values in lstOVData.The DataContext have the values like cmbOVUnits.DataContext = Count = 14.

So obviously cstrDisplayedOVUnitID value will be null.How can i solve this?

解决方案

Your first and main problem is: you work with data as with strings. Populate semantic data, not just strings. If you use the data in such controls in any way, the item object should better be not strings.

Combo box's list items can be any objects. The other problem is: the items can be not selected. You can check up, for example, SelectedIndex. If it returns the value < 0, nothing is selected. You can use this property to select an item programmatically. It is more reliable, because, for example, from looking at you code one cannot be sure that _cstrSelectdOVUnit is a valid object for selection, because I don't know if you add this object to the lost or not.

Please see and read carefully: ^].

—SA


这篇关于在组合框中选择一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆