How to pass to the Converter Parameter something that is not hard coded

风格不统一 提交于 2019-12-23 15:52:11

问题


Something like:

 <TextBlock Text="{Binding Text,Converter={StaticResource
  ccc},ConverterParameter=PersonName}"/>

when Person name is Property of the class for example.

Update:

I've seen a solution that tells to inherit from DependencyObject and to implement IValueConverter. I want to know if there is something simpler.


回答1:


The answer is straight-forward, but not what you want to hear.

You can only target a binding at DependencyProperty on a DependencyObject. Binding does not inherit from DO, so you can't binding the converter parameter.

If you want other state passed into a converter, you may have to subclass the desired obect and add new properties




回答2:


Have you looked into MultiBinding? If you want two properties sent to the converter, like "Text" and "PersonName" you may be able to do something like this:

<TextBlock>
  <TextBlock.Text>
    <MultiBinding Converter="{StaticResource ccc}">
      <Binding Path="Text"/>
      <Binding Path="PersonName"/>
    </MultiBinding>
  </TextBlock.Text>
</TextBlock>

This assumes that "Text" and "PersonNames" are properties on the DataContext. You may need to change the binding paths if that's not the case.



来源:https://stackoverflow.com/questions/6358094/how-to-pass-to-the-converter-parameter-something-that-is-not-hard-coded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!