我可以将整个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”。使用文档提供的示例确实很容易做到这一点。     

要回复问题请先登录注册