[英]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.