views:

319

answers:

1

I've written a MultiValueConverter which checks if a given list contains a given value and returns true if it does. I use it for binding to custom checkbox list. Now I'd like to write ConvertBack method so that if checkbox was checked, original value would be sent to the model. Is there a way to access values in ConvertBack method?

XAML:

<ListBox.ItemTemplate>
    <HierarchicalDataTemplate>
        <CheckBox Content="{Binding Path=Description}">
            <CheckBox.IsChecked>
                <MultiBinding Converter="{x:Static Classes:ListContainsMultiConverter.Instance}">
                    <Binding Path="Id" />
                    <Binding Path="DataContext.ContactTypes" RelativeSource="{RelativeSource AncestorType={x:Type Window}}" />
                </MultiBinding>
            </CheckBox.IsChecked>
        </CheckBox>
    </HierarchicalDataTemplate>
</ListBox.ItemTemplate>

I get correct results when I'm binding but is there a way to get the bound id when converting back? What I would like to achieve is that if checkbox is unchecked, the value would be removed from the list and if it is checked, the value would be added to the list.

+1  A: 

I solved my problem, and hopefully the solution will help you too. Take the multibinding you have, and instead of putting it in the IsChecked attribute, put it in the DataContext attribute. This may be where our issues differ... in my convert method, i was using the data in the bindings to grab an object, and then i returned myobject.text. I changed this to instead return just the object, so that it gets bound to the datacontext. I then bound the textbox.text property to the text property of myobject. it seems to work fine. You could then bind the list where values are removed to the checkbox.ischecked... I guess. I'm not exactly sure what you're trying to do.

I'm thinking this might get you on the right path...

<ListBox.ItemTemplate>
<HierarchicalDataTemplate>
    <CheckBox Content="{Binding Path=Description}">
        <CheckBox.DataContext>
            <MultiBinding Converter="{x:Static Classes:ListContainsMultiConverter.Instance}">
                <Binding Path="Id" />
                <Binding Path="DataContext.ContactTypes" RelativeSource="{RelativeSource AncestorType={x:Type Window}}" />
            </MultiBinding>
        </CheckBox.DataContext>
        <CheckBox.IsChecked>
            <Binding Path="Some_Property_For_IsChecked_In_Some_Object_In_The_Converter" />
        </CheckBox.IsChecked>
    </CheckBox>
</HierarchicalDataTemplate>

sub-jp