微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我可以将整个UI元素传递给IValueConverter吗?

如何解决我可以将整个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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?