开发者

WPF conditional convertor

I have binding to Image.Source and TextBlocks. Also I have own convertor classes.

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

For example:

<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.开发者_如何学CRow="2" Margin="2,2,2,2" FontSize="11" FontWeight="Normal"></TextBlock>

I want set condition for imageConvertor, for example:

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

It’s possible this write in XAML, maybe in convertor class?


Instead of making your ImageConverter an IvalueConverter, make it an 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>

A IMultiValueConverter is the same as an IValueConverter, except that it passes an array of objects instead of a single object value.

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

Here is is tough for me to guess on the design of your current converter, but this gives you a rough idea on what to do. I am implying from your question that if the status is not 0, you don't want your converter to return anything - hence the DependencyProperty.UnsetValue.


I don't think it's possible to do this in XAML. I'm pretty sure it's impossible to do (as is) in the converter because you don't have access to the sender (here a TextBlock) within.

EDIT : you can do it with a multivalue converter, because you need OneWay Binding. Multivalue converters are difficult to set up with a TwoWay binding (when you need the ConvertBack method).

What I would do would be to declare two Images (one with for each value of your TextBlock : 0 and else) and bind the visibility to the textblock Text value (or directly to Value.status).


This doesn't specifically answer the question, but I managed to solve my problem using this question as a guide, so I thought it might help future searchers. It could probably also be extended further to solve the original question with a little more work.

I was trying to find a way of evaluating an IF-conditional expression within XAML and wanted to combine the power of a Binding expression and a MarkupExtension. Using the idea of a converter, I managed to create a ConditionalMarkupConverter. This has two properties to specify the values to be returned when a binding expression evaluates to true or false, and since it's a converter, it can be easily added to a binding expression.

Converter

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}}" />

In this example, the TextBlock binds to a boolean property called IsActive and the converter then returns the string "Active" when IsActive is true, or "Inactive" when IsActive is false.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜