值转换器-在ConvertBack()方法中访问Convert()变量? [英] Value Converters - Access Convert() variables in ConvertBack() method?
问题描述
我有一个转换器,该转换器具有几个输入变量(一个对象和一个TextBox),然后返回TextBox.Text
String属性.
I have a converter which has a couple of input variables (an object and a TextBox) and then returns the TextBox.Text
String property.
我遇到的问题是转换器的ConvertBack()
方法.我无法将任何更新链接回该对象,因为我得到的只是一个字符串(文本框的文本).有什么方法可以访问某些(如果不是全部)Convert()
变量?或者至少知道哪个文本框正在调用ConvertBack()
?
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()
?
这是我的ItemsControl代码:
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;
推荐答案
您最好的选择是将一个私有属性添加到转换器类并在Convert期间存储您的值,以便ConvertBack可以访问它们.不过,您需要为每个绑定使用单独的转换器实例.
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
这篇关于值转换器-在ConvertBack()方法中访问Convert()变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!