0

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

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

非常感谢。

4

2 回答 2

0

你可以绑定到公共属性

 ItemsSource="{Binding _langListString}"

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

于 2013-05-21T14:09:43.457 回答
0

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

DataContext = _viewModelString;

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

这就是我要做的:

  1. 添加列表 _langListString = new List(); 到>模型视图。
  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 使事情变得非常复杂,没有调试的可能性。有人可以帮忙吗???

于 2013-05-21T15:30:11.387 回答