如何解决我可以将整个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>
ComboBox的地址与来自地理数据库的具有最高评分的值匹配。 Textblock具有用于匹配的用户输入地址。如果地址相同,我希望前景为绿色,否则为红色。
我以为也许我可以将整个TextBlock传递到dataconverter中,获取其Parent 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={\"Binding RelativeSource={x:Static RelativeSource.Self},Converter={x:StaticResource myConverter}}\" />
</StackPanel>
</DataTemplate>
是。请参阅msdn文章
, 您可以使用转换器将ComboBox
的SelectedItem
绑定,该转换器将其相等值与InputtedAddress
进行比较,并相应地返回returns5ѭ或Brushes.Red
。
棘手的部分是上述转换器需要以某种方式跟踪InputtedAdress
。这非常麻烦,因为我们无法使用ConverterParameter
进行绑定,因此我们需要一个复杂的转换器。
另一方面,使用IMultiValueConverter
可以更轻松地实现效果。例如:
<ComboBox Name=\"ComboBox\" ItemsSource=\"{Binding Path=MatchedAddressList}\" DisplayMemberPath=\"Address\" SelectedIndex=\"0\" SelectionChanged=\"ComboBox_SelectionChanged\"/>
<TextBlock Name=\"InputtedAddress\" Text=\"{Binding Path=InputtedAddress}\">
<TextBlock.Foreground>
<MultiBinding Converter=\"{StaticResource equalityToBrushConverter}\">
<Binding ElementName=\"ComboBox\" Path=\"SelectedItem\" />
<Binding Path=\"InputtedAddress\" />
</MultiBinding>
</TextBlock.Foreground>
</TextBlock>
然后,您将需要一个“ 9”来将两个传入的值转换为一个“ 12”。使用文档提供的示例确实很容易做到这一点。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。