WPF条件转换器

时间:2010-11-04 14:34:32

标签: wpf converter

我绑定了Image.Source和TextBlocks。我也有自己的转换器类。

<conv:StatusConvertor x:Key="statusConvertor"></conv:StatusConvertor>
<conv:ImageConvertor x:Key="imageConvertor"></conv:ImageConvertor>

例如:

<Image Source="{Binding Value.profilePhoto, Converter={StaticResource imageConvertor}}" Margin="4,4,4,2"/>

<TextBlock Name="tbStatus" Text="{Binding Value.status,Converter={StaticResource statusConvertor}}" Grid.Column="0" Grid.Row="2" Margin="2,2,2,2" FontSize="11" FontWeight="Normal"></TextBlock>

我想为imageConvertor设置条件,例如:

IF tbStatus.Text=="0"
THEN use imageConvertor on Image.Source

这可能是在XAML中写的,也许是在转换器类中?

3 个答案:

答案 0 :(得分:4)

不要将ImageConverter设为IvalueConverter,而是将其设为IMultiValueConverter:

<Image Margin="4,4,4,2">
    <Image.Source>
        <MultiBinding Converter="{StaticResource imageConvertor}">
            <Binding Path="Value.profilePhoto" />
            <Binding Path="Value.status" />
        </MultiBinding>
    </Image.Source>
</Image>

IMultiValueConverter与IValueConverter相同,只是它传递一个对象数组而不是一个对象值。

public object Convert(object[] values, 
                      Type targetType, object parameter, CultureInfo culture)
{
   // Use your converter code from before, but add a check for the Status value
   // as well
   string path = values[0].ToString();
   int status = Int32.Parse(values[1].ToString();

   if (status == 0)
      return newImageSource;

   return DependencyProperty.UnsetValue;
}

这对我来说很难猜测当前转换器的设计,但是这可以让您大致了解该怎么做。我暗示你的问题是,如果状态不是0,你不希望你的转换器返回任何东西 - 因此DependencyProperty.UnsetValue。

答案 1 :(得分:0)

我认为在XAML中不可能这样做。 我很确定在转换器中这样做是不可能的,因为你无法访问发送者(这里是一个TextBlock)。

编辑:您可以使用多值转换器,因为您需要OneWay绑定。使用TwoWay绑定很难设置多值转换器(当您需要ConvertBack方法时)。

我要做的是声明两个图像(一个用于TextBlock的每个值:0和其他)并将可见性绑定到文本块Text值(或直接绑定到Value.status)。

答案 2 :(得分:0)

这并没有具体回答这个问题,但我设法用这个问题作为指导来解决我的问题,所以我认为这可能对未来的搜索者有所帮助。它可能还可以进一步扩展,以通过更多的工作来解决原始问题。

我试图找到一种在XAML中评估IF条件表达式的方法,并希望结合Binding表达式和MarkupExtension的强大功能。使用转换器的想法,我设法创建了ConditionalMarkupConverter。这有两个属性来指定绑定表达式求值为truefalse时要返回的值,并且由于它是转换器,因此可以很容易地将其添加到绑定表达式中。

<强>转换器

public sealed class ConditionalMarkupConverter : MarkupExtension, IValueConverter
{
    public object TrueValue { get; set; }
    public object FalseValue { get; set; }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is bool && (bool)value)
        {
            // The given value has evaluated to true, so return the true value
            return TrueValue;
        }

        // If we get here, the given value does not evaluate to true, so default to the false value
        return FalseValue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

public override object ProvideValue(IServiceProvider serviceProvider)
{
    return this;
}

<强> XAML

<TextBlock Text="{Binding IsActive, Converter={converters:ConditionalMarkupConverter TrueValue=Active, FalseValue=Inactive}}" />

在此示例中,TextBlock绑定到名为IsActive的布尔属性,然后转换器在IsActivetrue时返回字符串“Active”,或者“非活动” “当IsActivefalse时。