user335160
user335160

Reputation: 1362

Binding data to ComboBox WPF

I am newbie to WPF, and needs help to bind data into the ComboBox. The xaml file contains the tag below.

<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

        }

and in the View code behind, I created a method to bind the data which contains a type of list

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

But it won't works,Any help please?

Upvotes: 1

Views: 3329

Answers (3)

skink
skink

Reputation: 5711

I assume your Inbox class consists of two properties (for simplicity), but there may be any number of them:

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

You write a DataTemplate, for example:

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

Then correct your ComboBox declaration like:

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

Finally you set DataContext of your ComboBox to your List<Inbox>:

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

EDIT: As you've asked for a simpler solution, you can just override ToString() method of your Inbox class:

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

Upvotes: 2

Debasis
Debasis

Reputation: 458

If your Inbox class is like,

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

And if you do not want to change your xmal, the code behind method should be like this,

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

Upvotes: 2

Vinit Sankhe
Vinit Sankhe

Reputation: 19885

Instead of DataContext={Binding} you should have ItemsSource={Binding}.

The data context for any frameworkelement in the visual tree is by default {Binding}.

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

Also for the combobox to display text of the items correctly I suppose you need DisplayMemberPath too. I assumed the property from Inbox class that you need to display is Name. Please replace with your relevant property name.

Upvotes: 2

Related Questions