0

I have a converter which has a couple of input variables (an object and a TextBox) and then returns the TextBox.Text String property.

The problem I'm running into is in the ConvertBack() Method of my converter. I have no way of linking any updates back to the object since all I get is a String (the Text of the Textbox). Is there some way I can access some (if not all) the Convert() variables? Or at least know which textbox is calling ConvertBack()?

Here is my ItemsControl Code:

<ItemsControl x:Name="ItemsControlGrid" ItemsSource="{Binding Path=ProjectOrganLocation.LesionTypes, Source={StaticResource Locator}}" >
    <ItemsControl.ItemsPanel>
         <ItemsPanelTemplate>
              <StackPanel Orientation="Horizontal" />
         </ItemsPanelTemplate>
     </ItemsControl.ItemsPanel>
     <ItemsControl.ItemTemplate>
         <DataTemplate>
              <TextBox Width="75" TextAlignment="Center" >
                   <TextBox.Text>
                         <MultiBinding Converter="{StaticResource LesionTypeConverter}"  >
                              <Binding RelativeSource="{RelativeSource AncestorType={x:Type TreeViewItem}}" Path="DataContext.OrganLocation"/>
                              <Binding RelativeSource="{RelativeSource Self}" Path="." />
                          </MultiBinding>
                    </TextBox.Text>
                </TextBox>
            </DataTemplate>
      </ItemsControl.ItemTemplate>
 </ItemsControl>

And here's a snippet from my Converter:

List<CategoryCode> Lesions = organ.getLesionTypes;

    if (organ.OrganDisplayName == organ.CurrentOrgan)
       organ.Count++;
    else
    {
       organ.Count = 0;
       organ.CurrentOrgan = organ.OrganDisplayName;
    }
return organ.Labels[organ.Count].LabelPrefix;
Joel B Fant
  • 24,406
  • 4
  • 66
  • 67
Saggio
  • 2,212
  • 6
  • 33
  • 50

2 Answers2

4

Your best bet would be to add a private property to the converter class and store your values during Convert so that ConvertBack can access them. You would need to use a separate instance of the converter for each binding though.

What are you trying to accomplish? There might be a better way to do it than a converter

Rachel
  • 130,264
  • 66
  • 304
  • 490
  • @Rachel - I have a Treeview and each item has a label and a dynamic amount of textboxes (based on the number of distinct values from a table) created by ItemsControl. Each textbox is tied to a property of the object and want the object to be updated as needed. How could I use separate instances? In my Convert() Method I am setting the TextBox.Name property to something unique for each TextBox if that makes a difference – Saggio Jun 17 '11 at 18:24
  • @user564636 What exactly is the converter for then? If I am understanding this right, you have an ItemsControl bound to a collection of `Objects`, and each `Object` has a collection of `Attributes`. Each Object is displayed using a Label and an ItemsControl bound to the Attributes. The Attribute ItemsControl displays each Attribute in a TextBox. Providing you are implementing `INotifyPropertyChanged` on your Objects and Attributes, and using two-way bindings, you shouldn't need a converter. – Rachel Jun 17 '11 at 18:35
  • @Rachel I just put relavent code snippet in OP. As you can see, since the textboxes are being created dynamically I need the converter to tell it which member of array to display – Saggio Jun 17 '11 at 18:48
  • @user56436 I'm still not sure I understand what is going on, but I think it could be accomplished more efficiently with a better model design. That being said, `ConvertBack` does not know of the parameters because the whole point of `ConvertBack` is to obtain the original parameter(s) from a converted value. If you choose to store your data in the converter class during Convert, your best bet is probably to create a new converter in the Loaded event of each TextBox and do the binding in the code-behind using the newly created converter. – Rachel Jun 17 '11 at 19:09
  • 1
    @Rachel Thanks for your help! I looked at my bindings again and reworked them so the converter wasn't necessary. Normal two-way binding now works properly. – Saggio Jun 17 '11 at 19:26
0

If you assign the bindings in your code behind, you can add a constructor to the converter which takes the sending TextBox (or any other piece of data) as a parameter and record it.

Ed Bayiates
  • 11,060
  • 4
  • 43
  • 62