【问题标题】:Define converter in xaml with parameter(s)使用参数在 xaml 中定义转换器
【发布时间】:2014-12-10 15:15:01
【问题描述】:

在我的 xaml 页面的开头,我这样定义转换器:

<Grid>
  <Grid.Resources>
    <l:MyMagicConverter x:Key="magicConverter"/>
  </Grid.Resources>

有没有办法在这个定义中为转换器添加一些参数?像 DependencyProperty,或者更简单的东西?像这样的:

    <l:MyMagicConverter x:Key="magicConverter" MyParameter="{Binding MyValue}"/>

我在使用时知道转换器参数,但我也想在定义中添加一些内容。

【问题讨论】:

    标签: .net xaml converter


    【解决方案1】:

    可以向转换器添加属性,然后在 Xaml 中访问它们。

    public class RadioButtonToIntConverter : IValueConverter
    {
        public string ABC { get; set; } 
    

    Xaml:

    <reportConverters:RadioButtonToIntConverter x:Key="RadioButtonToIntConverter" 
                                                ABC="def" />
    

    现在你将它绑定到什么,至于静态资源可能是个问题。


    顺便说一句,如果不想直接在 xaml 中实例化转换器,我提供了一种无需 xaml 实例化即可自动连接转换器的方法。

    Xaml: Call Binding Converter Without Defining StaticResource in Xaml Thanks to Markup Derived Base Class in C#

    我建议将此作为实例化目标转换器的一种方式,该目标转换器具有您可能想要使用的“预烘焙”属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-23
      • 2018-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-21
      相关资源
      最近更新 更多