简体   繁体   English

值转换器-在ConvertBack()方法中访问Convert()变量?

[英]Value Converters - Access Convert() variables in ConvertBack() method?

I have a converter which has a couple of input variables (an object and a TextBox) and then returns the TextBox.Text String property. 我有一个具有几个输入变量(一个对象和一个TextBox)的转换器,然后返回TextBox.Text String属性。

The problem I'm running into is in the ConvertBack() Method of my converter. ConvertBack()的问题是转换器的ConvertBack()方法中。 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? 有什么方法可以访问某些(如果不是全部) Convert()变量? Or at least know which textbox is calling ConvertBack() ? 或者至少知道哪个文本框正在调用ConvertBack()

Here is my ItemsControl Code: 这是我的ItemsControl代码:

<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;

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. 最好的选择是在Converter类中添加一个私有属性,并在Convert期间存储您的值,以便ConvertBack可以访问它们。 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 可能有比转换器更好的方法

如果您在后面的代码中分配绑定,则可以向转换器添加一个构造函数,该构造函数将发送文本框(或任何其他数据)作为参数并进行记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么使用 Convert 方法将 WPF combobox 中的值转换回? - Why Convert method is used to ConvertBack a value in WPF combobox? 获取 WPF 绑定中 IValueConverter 实现的 ConvertBack() 方法中的 Source 值 - Get the Source value in ConvertBack() method for IValueConverter implementation in WPF binding 图像到字节[],Convert和ConvertBack - Image to byte[], Convert and ConvertBack WPF:转换器的基类“抓住”了Convert方法 - WPF: Base class for converters “catches” Convert method 如何将第二个绑定值传递到同时用于Convert和ConvertBack的ValueConverter中? - How can you pass a second bound value into a ValueConverter that is used for both Convert and ConvertBack? 如何从Convert获取ConvertBack参数? - How to get in ConvertBack parameters from Convert? 什么时候调用ConvertBack方法? - When does ConvertBack method get called? IValueConverter 接口中的 ConvertBack 方法有什么用? - What is the use of ConvertBack method in IValueConverter interface? 在OneWay绑定上使用ConvertBack函数而不是Convert函数 - Using ConvertBack function on OneWay binding instead of the Convert function WPF 按钮绑定到属性 Convertback IMultiValueConverter 不会更新值 - WPF button binding to a property Convertback IMultiValueConverter does not update value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM