Makube

I use two comboboxes for data binding. My purpose is to bind the both Name(type string) properties of two business objects such as Client and Project. The result is strange that one combobox works quite well, but the other did not. I can not see the binding data string in the combobox(ie. The binding data did not display), however it seems they exist.

In Xaml, I use similar way to design two comboboxes, such as

<DataTemplate x:Key="ListTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=ProjectName}" />
</StackPanel>
</DataTemplate>

------------------------------------------------------------------------------------

<ComboBox x:Name="theCombo" IsSynchronizedWithCurrentItem="True" Width="200"
ItemsSource="{Binding}" ItemTemplate="{StaticResource ListTemplate}"/>

------------------------------------------------------------------------------------

In Code-behind, I use datatable as data source(the Client object is same way),

DataTable pdt = Project.GetProjectDataTable();
theCombo.DataContext = pdt;

-----------------------------------------------------------------------------------

Does anyone knows what is the problem on that Thanks for help



Re: Windows Presentation Foundation (WPF) Strange Problem on combobox data binding

Yiling Lai

This works well for me:

Code Snippet

namespace UntitledProject3
{
public class CData
{
private string _sClient;
private string _sProject;

public CData(string sClient, string sProject)
{
_sClient = sClient;
_sProject = sProject;
}

public string Client
{
get { return _sClient; }
set { _sClient = value; }
}
public string Project
{
get { return _sProject; }
set { _sProject = value; }
}
}

public class CDataList : ObservableCollection
{
public CDataList()
: base()
{
Add(new CData("11111", "AAAAA"));
Add(new CData("22222", "BBBBB"));
Add(new CData("33333", "CCCCC"));
Add(new CData("44444", "DDDDD"));
}
}

public partial class Window1
{
public Window1()
{
this.InitializeComponent();

// 在此 之下插入 建 象所需的代 。
LayoutRoot.DataContext = new CDataList();
}
}
}

Code Snippet
<Window
xmlns="
http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml/"
x:Class="UntitledProject3.Window1"
x:Name="Window"
Title="Window1"
Width="640" Height="480">

<Window.Resources>
<DataTemplate x:Key="ListTemplate1">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Project}" />
</StackPanel>
</DataTemplate>

<DataTemplate x:Key="ListTemplate2">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Client}" />
</StackPanel>
</DataTemplate>
</Window.Resources>

<Grid x:Name="LayoutRoot">
<ComboBox
Margin="277,125,194,0"
VerticalAlignment="Top"
Height="25.837"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource ListTemplate1}"/>
<ComboBox
Margin="277,188,194,0"
VerticalAlignment="Top"
Height="28.837"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource ListTemplate2}"/>
</Grid>
</Window>
If your problem still exist, please give more code.





Re: Windows Presentation Foundation (WPF) Strange Problem on combobox data binding

Makube

First, Thanks for answer. My case seems different from what you said.

In my data access layer, I use the DataSet to retrieve the data from database. The Project and Client are seperate business objects, in each of them I create List<T> class to assign the variable which comes from data access layer. Final, I provide List<T> to application layer for data binding the UIElement such as combobox in this case.

Before, I ever used the datatable(also driven from data access layer) for data binding, but one of two does not work well. Then I tried to use the List<T> instead of datatable, it seems works. Now I have two questions.

1. In previous answer, it used ObserableCollection<T> class. I also want to use that. So Is there any way to merge the List<T> and ObserableCollection<T> (I mean if possible to write other class for markup, pass the List<T> to ObserableCollection<T>, final get same result as the previous answer).

2. The datatable always does not works well, do you know the reason

Except for the code I have given above, my Project and Client object code is below

(In the database, The relationship between Project and Client is one to many)

//Project Object (The Client Object is the similar way to do)

public class Project

{
private int _ProjectID;
private String _ProjectName;
private DateTime _ProjectStartDate;
private int _ProjectClientID;

#region Public Properties

public int ProjectID
{
get { return _ProjectID;}

}

public String ProjectName
{
get { return _ProjectName;}
set { _ProjectName = value;}
}

public Client ProjectClient
{
get { return Client.GetClientByID(_ProjectClientID);}
set {_ProjectClientID = value.ClientID;}
}

public DateTime ProjectStartDate
{
get { return _ProjectStartDate;}
set {_ProjectStartDate = value;}
}

#endregion

#region User-defined Methods

public static List<Project> GetAllProjects()

{
return GetProjectList(ProjectGetway.GetAllProject());

//The parameter DataTable method is driven from data access layer

}

private static List<Project> GetProjectList(RMTDataSet.PROJECTDataTable dt)
{
List<Project> projects = new List<Project>();

foreach (RMTDataSet.PROJECTRow row in dt)
{
Project p = new Project();
p._ProjectName = row.ProjectName;
p._ProjectStartDate = row.ProjectStartDate;
p._ProjectID = row.ProjectID;
p._ProjectClientID = row.ClientID;
projects.Add(p);
}
return projects;
}

public static DataTable GetAllProjectDataTable()
{
return (DataTable)ProjectGetway.GetAllProject();
}

#endregion

---------------------------------------------------------------------------------------------------------------------------------------

In application layer, now I use the List<T> as datasource for data binding to combobox

List<Project> projects = Project.GetAllProjects();
myLeftCombo.DataContext = projects;

List<Client> clients = Client.GetAllClients();
myRightCombo.DataContext = clients;

---------------------------------------------------------------------------------------------------------------------------------------





Re: Windows Presentation Foundation (WPF) Strange Problem on combobox data binding

Yi-Lun Luo - MSFT

Hello, if you can, please use ObservableCollection<T> directly instead of a List<T>:

private static ObservableCollection<Project> GetProjectList(RMTDataSet.PROJECTDataTable dt)

{

ObservableCollection<Project> projects = new ObservableCollection<Project>();

foreach (RMTDataSet.PROJECTRow row in dt)

{

Project p = new Project();

p._ProjectName = row.ProjectName;

p._ProjectStartDate = row.ProjectStartDate;

p._ProjectID = row.ProjectID;

p._ProjectClientID = row.ClientID;

projects.Add(p);

}

return projects;

}

If you don’t want to change List< Project>, you can create a new class which derives from ObservableCollection< Project>, and use a loop to assign value from List to ObservableCollection.

Also, it’s always a good idea to separate UI from data accessing. Your current solution is better. Don’t try to bind to database directly on your UI.






Re: Windows Presentation Foundation (WPF) Strange Problem on combobox data binding

Makube

Thanks for solution. I have tried to use ObservableCollection<T> directly instead of List<T>, but now I meet one problem. As the solution mentioned above, first I add ''System.Collections.ObjectModel;'' in 'Classes' namespace, which stores all object classes(for example Project and Client objects in this case). Then I can't find ObservableCollection class for use (ie. System can not recognize this class).

But when I do the same thing in the application layer (the namespace is ''Application''), The ObservableCollection class exists and I can use that.So when I compile the project file, there is always error message occurs, which is ''type or namespace name 'ObservableCollection' could not be found (are you missing a using directive or an assembly reference )''

I am newer to 'ObservableCollection' class, can anyone tell me what happened, and how can I deal with it





Re: Windows Presentation Foundation (WPF) Strange Problem on combobox data binding

Yi-Lun Luo - MSFT

It is located in WindowsBase.dll. Have you referenced this assembly






Re: Windows Presentation Foundation (WPF) Strange Problem on combobox data binding

Makube

Thank you for your help, now it works