读书人

wpf combobox数据绑定有关问题

发布时间: 2012-05-28 17:59:54 作者: rapoo

wpf combobox数据绑定问题
我定义了两个基础类,ComboboxItemInfo 是用来绑定下拉框数据的。下面是我的基础类和页面绑定的代码。

C# code
 /// <summary>    /// Combobox 下拉框数据    /// </summary>    public class ComboboxItemInfo : INotifyPropertyChanged    {               private List<string> _itemDisplayInfo;        /// <summary>        /// 显示值        /// </summary>        public List<string> ItemDisylayInfo        {            get { return _itemDisplayInfo; }            set            {                if (_itemDisplayInfo != value)                {                    _itemDisplayInfo = value;                    NotifyPropertyChanged("ItemDisylayInfo");                }            }        }        private List<string> _itemValueInfo;        /// <summary>        /// 实际值        /// </summary>        public List<string> ItemValueInfo        {            get { return _itemValueInfo; }            set            {                if (_itemValueInfo != value)                {                    _itemValueInfo = value;                    NotifyPropertyChanged("ItemValueInfo");                }            }        }        //下拉框后面的textbox值        private string _itemTextBoxValue;        /// <summary>        /// Combobox后面的textbox值        /// </summary>        public string ItemTextBoxValue        {            get { return _itemTextBoxValue; }            set            {                if (_itemTextBoxValue != value)                {                    _itemTextBoxValue = value;                    NotifyPropertyChanged("ItemTextBoxValue");                }            }        }        //分组名称        private string _itemgroupName;        public string ItemGroupName        {            get { return _itemgroupName; }            set            {                if (_itemgroupName != value)                {                    _itemgroupName = value;                    NotifyPropertyChanged("ItemGroupName");                }            }        }        #region INotifyPropertyChanged        public event PropertyChangedEventHandler PropertyChanged;        protected void NotifyPropertyChanged(string propertyName)        {            if (this.PropertyChanged != null)            {                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));            }        }        #endregion    }    /// <summary>    ///     /// </summary>    public class KeyValue : INotifyPropertyChanged//, IDataErrorInfo    {        public int Index_Check = 0;        private List<ComboboxItemInfo> _comboBoxKeys;        /// <summary>        /// 每个子项的下拉框数据        /// </summary>        public List<ComboboxItemInfo> ComboBoxKeys        {            get { return _comboBoxKeys; }            set            {                if (_comboBoxKeys != value)                {                    _comboBoxKeys = value;                    NotifyPropertyChanged("ComboBoxKeys");                }            }        }        private  string _itemName;        /// <summary>        /// 每一项名称        /// </summary>        public string ItemName        {            get { return _itemName; }            set            {                if (_itemName != value)                {                    _itemName = value;                    NotifyPropertyChanged("ItemName");                }            }        }        //分组名称        private string _itemgroupName;        public string ItemGroupName        {            get { return _itemgroupName; }            set            {                if (_itemgroupName != value)                {                    _itemgroupName = value;                    NotifyPropertyChanged("ItemGroupName");                }            }        }        private Guid _evaluationItemId;        public Guid EvaluationItemId        {            get { return _evaluationItemId; }            set            {                if (_evaluationItemId != value)                {                    _evaluationItemId = value;                    NotifyPropertyChanged("EvaluationItemId");                }            }        }        private Visibility _isHaveText;        /// <summary>        /// 注明每一项后面是否包含textbox        /// </summary>        public Visibility IsHaveText        {            get { return _isHaveText; }            set            {                if (_isHaveText != value)                {                    _isHaveText = value;                    NotifyPropertyChanged("IsHaveText");                }            }        }        private string _textInfo;        /// <summary>        /// textbox后面的文本信息        /// </summary>        public string TextInfo        {            get { return _textInfo; }            set            {                if (_textInfo != value)                {                    _textInfo = value;                    NotifyPropertyChanged("TextInfo");                }            }        }        #region INotifyPropertyChanged        public event PropertyChangedEventHandler PropertyChanged;        protected void NotifyPropertyChanged(string propertyName)        {            if (this.PropertyChanged != null)            {                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));            }        }        #endregion    } 


C# code
 <ListBox ItemsSource="{Binding Path=PelvicFractureInfos_left[0].FingerKeys1, NotifyOnTargetUpdated=True,  ValidatesOnDataErrors=True}">                    <ListBox.Template>                        <ControlTemplate TargetType="{x:Type ListBox}">                            <WrapPanel Orientation="Vertical" IsItemsHost="True"/>                        </ControlTemplate>                    </ListBox.Template>                    <ListBox.ItemTemplate>                        <DataTemplate >                            <StackPanel Orientation="Horizontal">                                <Label Content="{Binding Path=ItemName}" Width="100" Height="25" ></Label>                                    <ComboBox ItemsSource="{Binding Path=ComboBoxKeys[0], NotifyOnTargetUpdated=True,  ValidatesOnDataErrors=True}" SelectedValuePath="{Binding Path=ComboBoxKeys[0].ItemValueInfo}" DisplayMemberPath="{Binding Path=ComboBoxKeys[0].ItemDisylayInfo}">                                        <ComboBox.ItemTemplate>                                            <DataTemplate>                                                <StackPanel>                                                    <TextBlock Text="{Binding ItemValueInfo}"/>                                                </StackPanel>                                            </DataTemplate>                                        </ComboBox.ItemTemplate>                                    </ComboBox>                            </StackPanel>                        </DataTemplate>                    </ListBox.ItemTemplate>                                     </ListBox>

PelvicFractureInfos_left[0].FingerKeys1这个属性是KeyValue类的对象。它里面包含了ComboboxItemInfo的一个列表,我下拉框的数据就是绑定的这个列表里的数据,现在目前数据都取到了,就是在下拉框中数据显示不了。
高手帮忙看下。谢谢

[解决办法]
public List<string> ItemValueInfo
{
get { return _itemValueInfo; }
set
{
_itemValueInfo = value;
NotifyPropertyChanged("ItemValueInfo");
}
}


[解决办法]
断点跟踪,注意value和display两个属性要对上号。
[解决办法]

我估计还是PelvicFractureInfos_left[0].FingerKeys1有问题
看下是不是实现IEnumerable接口,如List,数组等

读书人网 >C#

热点推荐