【问题标题】:wpf combobox binding is emptywpf 组合框绑定为空
【发布时间】:2013-05-21 13:50:22
【问题描述】:

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

我认为额外的困难是ComboBox是在资源中定义的,这里是资源代码:

<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>

我的数据对象定义如下:

public partial class Window1 : Window
{

    List<ComboBoxItemString> _langListString = new List<ComboBoxItemString>();

    // Object to bind the combobox selections to.
    private ViewModelString _viewModelString = new ViewModelString();


    public Window1()
    {
        // Localization settings
        _langListString.Add(new ComboBoxItemString()); _langListString[0].ValueString = "en-GB";
        _langListString.Add(new ComboBoxItemString()); _langListString[1].ValueString = "fr-FR";
        _langListString.Add(new ComboBoxItemString()); _langListString[2].ValueString = "en-US";


        // Set the data context for this window.
        DataContext = _viewModelString;


        InitializeComponent();


    }

还有模型视图:

/// This class provides us with an object to fill a ComboBox with
/// that can be bound to string fields in the binding object.
public class ComboBoxItemString
{
    public string ValueString { get; set; }
}


//______________________________________________________________________
//______________________________________________________________________
//______________________________________________________________________



/// 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
{
    /// Need a void constructor in order to use as an object element 
    /// in the XAML.
    public ViewModelString()
    {
    }

    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
}

我只是不知道该尝试什么。有没有人知道发生了什么,以及为什么组合框保持空白?

非常感谢。

【问题讨论】:

    标签: wpf binding combobox empty-list


    【解决方案1】:

    你可以绑定到公共属性

     ItemsSource="{Binding _langListString}"
    

    无法工作,因为 _langListString 不是公共属性

    【讨论】:

    • 非常感谢,我已公开属性“_langListString”,但组合框仍为空。
    【解决方案2】:

    根据我的分析,问题在于您的 DataContext。

    DataContext = _viewModelString;

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

    这就是我会做的:

    1. 添加列表 _langListString = new List();到 >ModelView。
    2. _langListString 将是 _viewModelString._langListString.add(Your Items) - 当您创建 _viewModelString 对象时,请小心设置 _langList。

    那么我认为其余的都会起作用。


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

    新的模型视图如下所示:

    /// 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();
        }
    

    我已经尝试了组合框中所有可能的组合(_langListString、_viewModelString._langListString、_viewModelString),但它不起作用:

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

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

    【讨论】:

    • 你好。你的属性必须有一个 setter 和一个 getter。像这样放: public List _langListString { get;放; }
    • Sexta13 和所有其他人,你真是天才!现在可以了。我不知道为什么,但这让你更加天才......非常非常感谢......
    • @user2405703,不客气 :) 投票给我+1 eheh ;) 你只需要考虑当你给控件提供数据上下文时,每个公共属性都至少有一个 getter(我认为setter 在双向绑定中是可选的和强制的),将可用于绑定。 :)
    • 现在,当我尝试投票 +1 时,我收到类似“需要 15 声望”的消息????我应该如何支持那些支持过我的人???
    • @user2405703 我想你可以把这个问题标记为正确答案……或者你接受这个作为正确答案:)
    猜你喜欢
    • 1970-01-01
    • 2012-06-18
    • 2011-03-09
    • 1970-01-01
    • 2015-09-15
    • 2018-01-17
    相关资源
    最近更新 更多