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

发布于 2024-11-15 16:58:32 字数 703 浏览 6 评论 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>

组合框具有与所选地理数据库中得分最高的地址相匹配的地址。文本块具有用于匹配的用户输入的地址。如果地址相同,我希望前景为绿色,否则为红色。

我想也许我可以将整个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

緦唸λ蓇 2024-11-22 16:58:32
<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 文章

<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

黑寡妇 2024-11-22 16:58:32

您可以使用转换器绑定到 ComboBoxSelectedItem,该转换器将其值与 InputtedAddress 进行比较并返回 Brushes.GreenBrushes.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>

然后您需要一个 IMultiValueConverter 将两个传入值转换为 Brush。使用文档非常容易做到这一点- 提供的例子。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文