wpf组合框绑定是空的

时间:2013-05-21 13:50:23

标签: wpf binding combobox empty-list

我正试图解决一些有困难的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
}

我只是不知道该怎么做。有人知道发生了什么,为什么组合框保持空白?

非常感谢。

2 个答案:

答案 0 :(得分:0)

你可以绑定到公共属性

 ItemsSource="{Binding _langListString}"

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

答案 1 :(得分:0)

  

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

     

DataContext = _viewModelString;

     

如果将viewModelString赋予DataContext,则必须在其中定义_langListString&gt;,以便组合框知道它绑定到哪个项目。

     

这就是我要做的事情:

     
      
  1. 添加列表_langListString = new List();到&gt; ModelView。
  2.   
  3. _langListString将是_viewModelString._langListString.add(您的项目) - 当您创建_viewModelString对象时,请小心设置_langList。
  4.         

    然后我认为剩下的就行了。


非常感谢,我有你建议的更改,但这个组合框仍然是空的: - (

新模型视图如下所示:

/// 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使得事情变得非常复杂而没有调试的可能性。有人可以帮忙???