值转换器 - 在 ConvertBack() 方法中访问 Convert() 变量?
我有一个转换器,它有几个输入变量(一个对象和一个 TextBox),然后返回 TextBox.Text
String 属性。
我遇到的问题是在我的转换器的 ConvertBack()
方法中。我无法将任何更新链接回该对象,因为我得到的只是一个字符串(文本框的文本)。有什么方法可以访问一些(如果不是全部)Convert()
变量吗?或者至少知道哪个文本框正在调用 ConvertBack()
?
这是我的 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>
这是我的转换器的片段:
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;
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最好的选择是将私有属性添加到转换器类并在 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
如果您在后面的代码中分配绑定,则可以向转换器添加一个构造函数,该构造函数将发送的 TextBox(或任何其他数据)作为参数并记录它。
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.