开发者

Can I pass entire UI element into a IValueConverter?

开发者 https://www.devze.com 2023-03-13 12:07 出处:网络
<DataTemplate> <StackPanel Orientation=\"Vertical\" Name=\"AddressStackPanel\" > <ComboBoxName=\"ComboBox\" ItemsSource=\"{Binding Path=MatchedAddressList}\" DisplayMemberPath=\"Address
<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 }  />
 开发者_JS百科 </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


<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> 

Yes. See msdn article


You can bind to the SelectedItem of the ComboBox using a converter that compares its value for equality to the InputtedAddress and returns Brushes.Green or Brushes.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 use ConverterParameter 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:

<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>

You would then need an IMultiValueConverter to convert the two incoming values to a Brush. This is really easy to make using the documentation-provided example.

0

精彩评论

暂无评论...
验证码 换一张
取 消