Reputation: 2952
I am working on a project which allows tagging of directories. As part of the interface, I would like to display a list of the directories. Each entry in the list would be the directory name, plus a number of columns. One for each tag known to the application at the time, with a checkbox indicating whether the directory has been tagged with that tag or not. The user can then tag or untag a number of directories easily.
I thought using a WPF DataGrid would be good for this, but am having trouble deciding how to store the tagged directory in a way that allows me to bind it to the DataGrid instance easily, since the number & makeup of the tags could change at any time.
One way would be to have a master list of directories & their tags, and from this I could generate a complementary set of Expando objects every time the number of known tags changed. Each object would have a boolean property for each tag. The list of these objects could be bound to the DataGrid and autogenerated columns would display them automatically. The nice thing about this is that it does model what I am displaying nicely - even if the directory doesn't have a tag, it still has the property of "doesn't have this tag", and its simple to implement. The problems are its messy and inefficient, and (not sure about this) I don't think I can call methods on the Expando object when the properties change.
The other way would be to dynamically re-create a DataTemplate whenever the known tags changed. The downside is I have to dynamically create a DataTemplate in code-behind.
I wonder though thinking about the first option - is there a way to override the properties an object appears to have to WPF?
For example, is there an interface like IDataBindingSource
which looks something like
interface IDataBindingSource {
string[] MyProperties();
Type[] MyPropertyTypes();
object[] MyPropertyValues();
}
That is, is there a simple way to change what properties appear to the DataGrid (and so use autogenerated columns) at runtime?
EDIT: To clarify about the tags. By tags I mean the small descriptive string version. The number of tags the user will want is limited, but there are a large number of directories. So I would like to display a set of checkboxes for each directory that would allow me to add or remove a tag for that directory, just like you would set permissions for users, for example (each permission being a tag, user's the directory). The number of potential tags at any given time though is variable, as the user could add more, or remove a tag from all directories and effectively deleting it.
This isn't a hard problem - a really naive way would simply be to manually draw a table, one column for the directory, the others with the known list of tags, and then logic would create and set checkboxes in the cells with loops. There are many ways to do it - but I would like to use the nicest - that allows me to code it quickly and preserves functionality inbuilt into controls like freezing and reordering, etc.
Upvotes: 0
Views: 218
Reputation: 12858
I've never been a fan of the DataGrid
control, especially not in WPF. Considering how versatile WPF is with control templating and styling, I'd go with the ListView
using a GridView
as the content (ListView.View
).
Then you can scrap the whole convoluted IDataBindingSource
bit and go straight MVVM, which is much easier with standard bindings.
For example, you could have a DirectoryModel
class that would have something like this:
public sealed class DirectoryModel : INotifyPropertyChanged
{
public ObservableCollection<DirectoryModel> Subdirectories { get; set; }
// Notify when this property changes for proper binding behavior!
public bool IsTagged { get; set; }
}
Then your ListViewItem.IsSelected
property would be bound to DirectoryModel.IsTagged
.
Upvotes: 1