【问题标题】:Binding data to ComboBox WPF将数据绑定到 ComboBox WPF
【发布时间】:2011-10-24 03:18:15
【问题描述】:

我是 WPF 的新手,需要帮助将数据绑定到 ComboBox。 xaml 文件包含以下标记。

<UserControl x:Class="SKAT.Postfordeler.Client.UI.View.ChooseInboxView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="42" d:DesignWidth="598">


<Grid>
    <StackPanel Orientation="Horizontal">
        <ComboBox Name="_currentInbox" Width="180"  Margin="5" Height="22" DataContext="{Binding}"  />
        <Label Content="Et job kører allerede i denne indbakke (1500 ud af 1700 poster behandlet)" Name="_currentProcess"  Margin="5" Height="25" />
    </StackPanel>

</Grid>

//Inbox class , this class was implemented in seperate project
namespace SKAT.Postfordeler.Shared.DataTypes
{
   [DataContract]
   public class Inbox
    {
       [DataMember]
       public String Id { get; set; }
       [DataMember]
       public String Folder { get; set; }
       [DataMember]
       public Rule Rules { get; set; }
    }
}

//This code is located in the controller, the Activate method will fire when the MainWindow was executed

 public void Activate()
        {
            var configuration = _configurationManager.GetConfiguration();// this method gets the xaml file settings

            _chooseInboxView.FillInboxes(configuration.Inboxes); // Inboxes data binds to combobox

        }

在后面的查看代码中,我创建了一个方法来绑定包含列表类型的数据

public void FillInboxes(List<Inbox> inboxes)
{
   DataContext = inboxes;
}

但它不起作用,请帮忙?

【问题讨论】:

  • Inbox 是如何实现的?另外,尝试绑定您的 ComboBox 的 ItemsSource 而不是 DataContext
  • 收件箱被实现为一个包含属性的类

标签: wpf


【解决方案1】:

我假设您的 Inbox 类包含两个属性(为简单起见),但它们可能有任意数量:

public class Inbox
{
    public int ID { get; set; }
    public string Text { get; set; }
}

你写一个DataTemplate,例如:

<Grid.Resources>
    <DataTemplate x:Key="InboxTemplate">
        <WrapPanel>
            <TextBlock Text="{Binding Path=ID}"/>
            <TextBlock>:</TextBlock>
            <TextBlock Text="{Binding Path=Text}"/>
        </WrapPanel>
    </DataTemplate>
</Grid.Resources>

然后更正您的 ComboBox 声明,如:

<ComboBox Name="_currentInbox" Width="180"  Margin="5" Height="22" ItemsSource="{Binding}" ItemTemplate="{StaticResource InboxTemplate}" />

最后,您将 ComboBox 的 DataContext 设置为您的 List&lt;Inbox&gt;

public void FillInboxes(List<Inbox> inboxes)
{
   _currentInbox.DataContext = inboxes;
}

编辑:正如您所要求的更简单的解决方案,您可以覆盖 ToString() 类的 ToString() 方法:

protected override string ToString()
{
    return ID.ToString() + ":" + Text;
}

【讨论】:

  • 我不想在我的 xaml 文件中添加一些额外的代码,只是一个简单的。 :) ,这也行不通....
  • @user335160,我已经测试了DataTemplate 方法,它对我有用。你可能想展示你做了什么,所以我可以看到问题。检查我帖子的编辑版本,我添加了一个更简单的解决方案。
  • 非常好。很好的例子+解释。
【解决方案2】:

您应该使用ItemsSource={Binding},而不是DataContext={Binding}

可视化树中任何框架元素的数据上下文默认为{Binding}

 <ComboBox Name="_currentInbox"
      SelectedItem="Hoved"
      Width="180"
      Margin="5"
      Height="22"
      DisplayMemberPath="Name"
      ItemSource="{Binding}" /> 

为了让组合框正确显示项目的文本,我想你也需要DisplayMemberPath。我假设您需要显示的来自Inbox 类的属性是Name。请替换为您的相关属性名称。

【讨论】:

  • 我对此进行了测试,但我不知道为什么不起作用。我粘贴了整个 xaml 代码供您参考。数据上下文已经有一个值,所以这有点奇怪。
  • 请检查您的Output 窗口。它一定会显示一些绑定错误。
【解决方案3】:

如果你的收件箱类是这样的,

public class Inbox
{
    public int ID { get; set; }
    public string Text { get; set; }
}

如果你不想改变你的xmal,方法后面的代码应该是这样的,

public void FillInboxes(List<Inbox> inboxes) 
    {
        _currentInbox.DisplayMemberPath = "Text"; // To display the 'Text' property in the combobox dropdown
        //_currentInbox.DisplayMemberPath = "ID"; // To display the 'ID' property in the combobox dropdown
        _currentInbox.DataContext = inboxes; 
    }

【讨论】:

  • 我对此进行了测试,但我不知道为什么不起作用。我粘贴了整个 xaml 代码供您参考。数据上下文已经有一个值,所以这有点奇怪。
  • 在这种情况下,您可以尝试,_currentInbox.DataContext = null,然后将 DataContext 分配给组合框
  • @Debasis- 仍然无法正常工作,arggggggg,我不知道发生了什么,我创建了一个简单的实现及其工作,但是当我尝试在我的项目中应用它时它没有工作...哇哦
  • 然后做一件事,从您的 xaml 和后面的代码中删除 DataContext="{Binding}",而不是 _currentInbox.DataContext = inboxes,尝试 _currentInbox.ItemSource = inboxes
  • 希望您的服务没有返回空列表..!!
猜你喜欢
  • 2016-04-30
  • 1970-01-01
  • 1970-01-01
  • 2011-05-06
  • 2019-06-23
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
  • 2011-02-06
相关资源
最近更新 更多