我可以将整个 UI 元素传递到 IValueConverter 中吗?
<DataTemplate>
<StackPanel Orientation="Vertical" Name="AddressStackPanel" >
<ComboBox Name="ComboBox" ItemsSource="{Binding Path=MatchedAddressList}" DisplayMemberPath="Address" SelectedIndex="0" SelectionChanged="ComboBox_SelectionChanged"/>
<TextBlock Name="InputtedAddress" Text="{Binding Path=InputtedAddress}" Foreground={Hopefully pass the UI element to the dataconverter } />
</StackPanel>
</DataTemplate>
组合框具有与所选地理数据库中得分最高的地址相匹配的地址。文本块具有用于匹配的用户输入的地址。如果地址相同,我希望前景为绿色,否则为红色。
我想也许我可以将整个 TextBlock 传递到数据转换器中,获取其父 StackPanel,获取子 0,转换为 Combobox 获取第 0 个元素并进行比较,然后返回红色或绿色。这可行吗?
否则我想我必须遍历视觉树,它和我认为的一样丑陋
<DataTemplate>
<StackPanel Orientation="Vertical" Name="AddressStackPanel" >
<ComboBox Name="ComboBox" ItemsSource="{Binding Path=MatchedAddressList}" DisplayMemberPath="Address" SelectedIndex="0" SelectionChanged="ComboBox_SelectionChanged"/>
<TextBlock Name="InputtedAddress" Text="{Binding Path=InputtedAddress}" Foreground={Hopefully pass the UI element to the dataconverter } />
</StackPanel>
</DataTemplate>
The ComboBox has addresses matched by from a geodatabase with the highest scoring value selected. The Textblock has the user-inputted address that was used for matching. If the address is the same, I want the foreground to be Green, otherwise Red.
I thought maybe I could pass the entire TextBlock into the dataconverter, get its Parent StackPanel, get child 0, cast to a Combobox get the 0th element and compare, and then return red or green. Is this do-able?
Otherwise I think I have to traverse the visual tree which is just as ugly i think
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的。请参阅 msdn 文章
Yes. See msdn article
您可以使用转换器绑定到
ComboBox
的SelectedItem
,该转换器将其值与InputtedAddress
进行比较并返回Brushes.Green
或Brushes.Red
相应地。棘手的部分是上面提到的转换器需要以某种方式跟踪
InputtedAdress
;这是相当麻烦的,因为我们不能使用ConverterParameter来绑定,所以我们需要一个稍微复杂的转换器。另一方面,使用
IMultiValueConverter
可以更轻松地实现该效果。例如:然后您需要一个
IMultiValueConverter
将两个传入值转换为Brush
。使用文档非常容易做到这一点- 提供的例子。You can bind to the
SelectedItem
of theComboBox
using a converter that compares its value for equality to theInputtedAddress
and returnsBrushes.Green
orBrushes.Red
correspondingly.The tricky part is that the converter mentioned above would need to keep track of
InputtedAdress
somehow; this is quite cumbersome because we can't useConverterParameter
to bind, so we 'd need a somewhat involved converter.On the other hand, the effect can be implemented more easily with an
IMultiValueConverter
. For example:You would then need an
IMultiValueConverter
to convert the two incoming values to aBrush
. This is really easy to make using the documentation-provided example.