views:

605

answers:

1

I am currently stuck at a dead end with the following situation:

I have a List<Category> collection with each Category containing an Id, Name and a List<string[]> property called Subcategory (where array contains the Name and Id of that subcagtegory - I didn't feel like creating another class).

I have a TreeView with HierarchicalDataTemplate bound properly to categories and subcategories. I also have check boxes next to the tree node and I bind IsChecked to a MultiBinding of the Id of the sub/category and the an overall list of sub/categories that should be checked-off.

<CheckBox.IsChecked>
    <MultiBinding Converter="{StaticResource IsCategoryChecked}">
        <Binding Path="Id"/>
         <Binding Path="myDataSet.Tables[Categories]/cat_subcat"/>
    </MultiBinding>
</CheckBox.IsChecked>

The converter I use simply checks if the name of the sub/category I provide is part of the list (both are values I'm binding to). This works fine and overall I am quite happy with the result.

The problem comes with converting the check/uncheck back. When user changes the IsChecked value I must either add the Id of the sub/category to the list of all checked-off categories or remove that Id from it. Unfortunately when I've attempted to implement the ConvertBack() method for IMultiValueConverter, I only have access to the ACTUAL value that changed (the true or false value of IsChecked) and have NO access to the Id of the sub/category that THAT SPECIFIC CHECKBOX is bound to.

SOLUTIONS I'VE TRIED but failed miserably:

  1. Saving the sub/category name when I do the initial conversion is not an option since I'm using the same Converter for ALL the sub/categories and thus I'd only save the last sub/category I've tried to convert.

  2. Using a MouseUp/KeyUp event is useless because (for whatever reason) they fail to be triggered (a bug?).

I am contemplating to ignore the ConvertBack() and use Checked/Unchecked events instead but feel like that is a rather "dirty" solution because I might just as well ignore the bindings altogether! Is this the only way to go? I've ran out of options but still hope for a "good" solution, if it's out there!

+1  A: 

You have a couple of options, and I've used both in production:

  1. Overload Checked/Unchecked, as you've said, which gives you the most control over the situation
  2. Add an IsCategoryChecked property to your strongly typed table in the code behind which handles the changes required behind the scenes, and you bind to the IsCategoryChecked property rather than using the converter

The second one works fairly well thanks to the partial classing with strongly typed datasets. However, it still isn't "seamless".

sixlettervariables
Your second suggestion seems interesting. I have one question about it though. I need a TwoWay binding, meaning that it has to determine whether to check off based on the bound data (works well now) AND it also has to edit the data if user checks off. How would I handle the Target -> Source change?
Alexandra
I didn't realize you'd just used a String for the subcategories, which produces a bit of a rub where you can't exactly ConvertBack. Basically, if you had a fullblown Category class that could either be a cat or subcat, they would know their parent and could handle Target->Source.
sixlettervariables