wpf组合框绑定为空

wpf组合框绑定为空,wpf,binding,combobox,empty-list,Wpf,Binding,Combobox,Empty List,我正在努力解决一些困难的wpf。这个组合框似乎是一个非常基本的问题,但即使在阅读了所有可能的类似帖子之后,我也无法填充它 我认为额外的困难是,组合框是在资源中定义的,以下是资源代码: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml

我正在努力解决一些困难的wpf。这个
组合框
似乎是一个非常基本的问题,但即使在阅读了所有可能的类似帖子之后,我也无法填充它

我认为额外的困难是,
组合框
是在资源中定义的,以下是资源代码:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:s="clr-namespace:DiagramDesigner">

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="Styles/Shared.xaml"/>
    <ResourceDictionary Source="Styles/ToolBar.xaml"/>
</ResourceDictionary.MergedDictionaries>

<ToolBar x:Key="MyToolbar" Height="120">

    <!--Languages-->
    <GroupBox Header="Localization" Style="{StaticResource ToolbarGroup}" Margin="3">
        <Grid>
            <ComboBox Height="23" HorizontalAlignment="Center" 
                      VerticalAlignment="Top"  Width="120"
                      ItemsSource="{Binding _langListString}" 
                        DisplayMemberPath="ValueString" 
                        SelectedValuePath="ValueString" 
                        SelectedValue="{Binding LangString}"
                      />
        </Grid>
    </GroupBox>

  </ToolBar>
我只是不知道还能尝试什么。是否有人知道发生了什么,以及为什么组合框保持为空


非常感谢。

您只需绑定到公共属性即可

 ItemsSource="{Binding _langListString}"
无法工作,因为_langListString不是公共属性

根据我的分析,问题在于您的数据上下文

DataContext=\u viewModelString

如果将viewModelString指定给DataContext,则必须在其中定义_langListString>,以便组合框知道它绑定到哪个项

这就是我要做的:

  • 添加列表_langListString=新列表();到>模型视图
  • _langListString将是_viewModelString._langListString.add(您的项目)–创建_viewModelString对象时,请小心地重新设置_langList
  • 那我想剩下的就行了


    非常感谢,我有您建议的更改,但此组合框仍然为空:-(

    新的modelview如下所示:

    /// Class used to bind the combobox selections to. Must implement 
    /// INotifyPropertyChanged in order to get the data binding to 
    /// work correctly.
    public class ViewModelString : INotifyPropertyChanged
    {
        public List<ComboBoxItemString> _langListString {get;set;}
        /// Need a void constructor in order to use as an object element 
        /// in the XAML.
        public ViewModelString()
        {
            // Localization settings
            _langListString = new List<ComboBoxItemString>();
            ComboBoxItemString c;
            c = new ComboBoxItemString(); c.ValueString = "en-GB"; _langListString.Add(c);
            c = new ComboBoxItemString(); c.ValueString = "fr-FR"; _langListString.Add(c);
            c = new ComboBoxItemString(); c.ValueString = "en-US"; _langListString.Add(c); 
        }
    
        private string _langString = "en-GB";
    
        /// String property used in binding examples.
        public string LangString
        {
            get { return _langString; }
            set
            {
                if (_langString != value)
                {
                    _langString = value;
                    NotifyPropertyChanged("LangString");
                }
            }
        }
    
        #region INotifyPropertyChanged Members
    
        /// Need to implement this interface in order to get data binding
        /// to work properly.
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        #endregion
    }
    
    我在组合框中尝试了所有可能的组合(_langListString,_viewModelString._langListString,_viewModelString),但它不起作用:

    <ComboBox Height="23" HorizontalAlignment="Center" 
                          VerticalAlignment="Top"  Width="120"
                          ItemsSource="{Binding _langListString}" 
                            DisplayMemberPath="ValueString" 
                            SelectedValuePath="ValueString" 
                            SelectedValue="{Binding LangString}"
                          />
    
    
    

    我倾向于认为这个xaml使事情变得非常复杂,没有调试的可能性。有人能帮忙吗?

    非常感谢,我已经公开了属性“\u langListString”,但组合框仍然是空的。您好。您的属性必须有一个setter和getter。如下所示:public List \u langListString{get;set;}Sexta13和所有其他人,你只是天才!现在起作用了。我不知道为什么,但这让你更加天才……非常非常感谢……@user2405703,不客气:)投票给我+1 ehh;)您只需要认为,当您为控件提供datacontext时,每个至少有一个getter的公共属性(我认为setter在双向绑定中是可选的和必需的)都可以用于绑定。:)现在,当我试着投+1票时,我得到了一个类似“需要15个声誉”的信息????我应该如何支持那些支持我的人?@user2405703我想你可以把这个问题标记为正确答案……或者你接受这个作为正确答案:)
    /// Class used to bind the combobox selections to. Must implement 
    /// INotifyPropertyChanged in order to get the data binding to 
    /// work correctly.
    public class ViewModelString : INotifyPropertyChanged
    {
        public List<ComboBoxItemString> _langListString {get;set;}
        /// Need a void constructor in order to use as an object element 
        /// in the XAML.
        public ViewModelString()
        {
            // Localization settings
            _langListString = new List<ComboBoxItemString>();
            ComboBoxItemString c;
            c = new ComboBoxItemString(); c.ValueString = "en-GB"; _langListString.Add(c);
            c = new ComboBoxItemString(); c.ValueString = "fr-FR"; _langListString.Add(c);
            c = new ComboBoxItemString(); c.ValueString = "en-US"; _langListString.Add(c); 
        }
    
        private string _langString = "en-GB";
    
        /// String property used in binding examples.
        public string LangString
        {
            get { return _langString; }
            set
            {
                if (_langString != value)
                {
                    _langString = value;
                    NotifyPropertyChanged("LangString");
                }
            }
        }
    
        #region INotifyPropertyChanged Members
    
        /// Need to implement this interface in order to get data binding
        /// to work properly.
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        #endregion
    }
    
      // Object to bind the combobox selections to.
        private ViewModelString _viewModelString;
    
        public Window1()
        {
            // Set the data context for this window.
            _viewModelString = new ViewModelString();
            DataContext = _viewModelString;
    
            InitializeComponent();
        }
    
    <ComboBox Height="23" HorizontalAlignment="Center" 
                          VerticalAlignment="Top"  Width="120"
                          ItemsSource="{Binding _langListString}" 
                            DisplayMemberPath="ValueString" 
                            SelectedValuePath="ValueString" 
                            SelectedValue="{Binding LangString}"
                          />