I have created a data grid in WPF and have 2 lists. I want to bind one column to one list and rest of the columns to another list.
Can anyone please tell me how to do this?
Thanks
Basically, You cant. The datagrid is an ItemsControl which has one ItemsSource property.
What i would do is build a view model which is a composite object that contains one of each of the items (from the two lists). then you could bind to a collection of these.
public class CompositeItem{
public Object ItemFromListOne { get; set; }
public Object ItemFromListTwo { get; set; }
}
Related
I am trying to add polygons to the telerik rad map with a PolygonData class embedded within a parent class.
public class PolygonClass
{
public int ID { get; set; }
public PolygonData Polygon { get; set; }
}
I'm trying to bind my VisualizationLayer.ItemSource to the 'Polygon' Property within 'PolygonClass' because I need to reference an ID value in various other places of my application. The only way I've recently been able to make this work is by binding to a property of ObservableCollection.
Has anyone else ran into this and knows of a solution? Thanks in advance.
There are several ways to achieve your requirement.
You can populate the Items collection of VisualizaztionLayer (instead of ItemsSource) with PolygonData objects. Any additional information, like the the ID, can be stored in the ExtendedData property of the PolygonData.
PolygonData polygon = new PolygonData();
polygon.ExtendedData["ID"] = 3;
Or you can use the map bindable wrappers inside the ItemTemplate property of VisualizationLayer. In your case the wrapper that should be used is MapPolygonView.
<telerik:VisualizationLayer.ItemTemplate>
<DataTemplate>
<telerik:MapPolygonView Points="{Binding Polygon.Points}" />
</DataTemplate>
</telerik:VisualizationLayer.ItemTemplate>
Note that in this case, you won't need the PolygonData in your PolygonClass, because the MapPolygonView will automatically generate one and add in the VisualizationLayer. Instead, you can define the shape information (like points and fill) in PolygonClass and data bind to those.
I have a ListBox control that contains the names of files inside a directory.
How would I iterate though the controls and get those names? I've tried:
for (int i = 0; i < listboxFileGroups.Items.Count; i++)
{
// I don't want to use properties that start with Selected
// Here is what I was looking for
string textItem = listboxFileGroups.Items[i].ToString();
}
Any suggestions?
You may want to explore an MVVM approach.
In one of your view model classes, you can have an ObservableCollection<string>:
public ObservableCollection<string> StuffForListBox { get; set; }
Populate that ObservableCollection with what you want to get displayed in the ListBox.
In the code-behind of the UserControl or Window in which you have the ListBox, set the DataContext to an instance of the class containing StuffForListBox seen above.
this.DataContext = new MyClass();
Alternatively you could also create a datatemplate for the usercontrol / window which will automagically wire up the datacontext with your view model.
Since you only mentioned that you want to display the files in a directory (not including sub-directories), you just need to bind the ItemsSource to StuffForListBox.
<ListBox ItemsSource="{Binding StuffForListBox}" ... >
To iterate through the strings displayed in the ListBox you just need to iterate through the ObservableCollection.
If you don't want to bother with MVVM or if that is some third party listbox, you can try grabbing the ItemsSource in the codebehind and loop through that but I'd certainly recommend MVVM. It'll make your life easier.
Now, if you wanted to get a little crazier and display things like subfolders then an ObservableCollection<string> won't cut it. You would need to create a class that contains children to model how a folder has files and subfolders.
public class DemoItem
{
public string Name { get; set; }
public DemoItem Parent { get; set; }
public ObservableCollection<DemoItem> Children { get; set; }
public bool IsSelected { get; set; }
}
...and then base your observable collection thats bound to the listbox on the above class.
If and when you do that, your listbox won't display the items properly until you create a DataTemplate But I suppose that't outside of the scope of the question :p
I have a database which is created using EF4.1 code first. The data context is as follow:
public DbSet<User> Users { get; set; }
public DbSet<Address> Addresses { get; set; }
I want to show address for a user by binding the users.Address to a datagrid in a ViewModel scenario. The problem is that I cannot convert Users.Address into a list that I can bind a collection into it. Any suggestion?
Here are some links on how to use the Local property of DbSet<> for data binding.
Using DbContext in EF 4.1 Part 7: Local Data
EF Feature CTP5: Code First Model with Master-Detail WPF Application
I have a DataGrid with a combobox inside a template column. Elsewhere on this screen, the user makes a 'customer' selection from a separate control altogether. In order to populate the comboboxes in my datagrid, I need to pass in that selected customer as a parameter, in addition to other information from each row in the grid.
Basically... the grid contains part information, and the combobox items are based on a combination of the following: selected customer, part number, and manufacturer. Each row's combobox can potentially have a different source list. Is there a way I can bind the ItemsSource for that combobox in XAML?
I may not understand correctly, but you could possibly have an object that contains all that information together, and bind that to the combo box.
ie
public class ContextualInfo
{
public Customer Customer { get; set; }
public int PartNumber { get; set; }
public Manufacturer Manufacturer { get; set; }
}
In reply to comment.
How about having the rows returned from the query to also be in the ContextualInfo mentioned above? You can then bind the itemsource to that. You could potentially run the query in the constructor for the ContextualInfo class.
What is the best approach to bind a WPF DataGrid ItemsSource to an ObservableCollection of ObservableCollections ?
e.g.
public ObservableCollection<ObservableCollection<MyDataItem>> DataValues = new ObservableCollection<ObservableCollection<MyDataItem>>();
where MyDataItem may look like this:
public class MyDataItem
{
public string Caption { get; set; }
public string DataValue { get; set; }
}
I can assume the collection of collections isn't jagged, and they all contain the same number of "columns"
Is it possible to bind each column dynamically to the 'DataValue' property of the MyDataItem objects or do I need to pack the data into an easier structure to bind to?
This is possible by using a collection-derived class as the inner object, and implementing ICustomTypeDescriptor on it - See a similar SO question on the topic (tagged with Silverlight, but same idea)
Silverlight DataGrid - Binding to a Collection of Collections of objects.