Adam
Adam

Reputation: 1493

MultiBinding Converter not binding to TextBlock in DataTemplate

I have a DataGrid with multiple columns and in one of the columns I'm using MultiBinding to bind to a MultiValueConverter, but nothing in showing up in that column. I am not sure what I am doing wrong. The ItemSource for the DataGrid, MfrSelection, does not contain an object corresponding to the VolumeToPercentConverter. Is this what is messing up the binding?

Here is the relevant xaml

    <Window.Resources>
        <local:VolumeToPercentConverter x:Key="VolumeToPercentConverter"/>
    </Window.Resources>
    ...        
<DataGrid x:Name="_mfrSelectionGrid" Grid.ColumnSpan="2" Grid.Row="2" ItemsSource="{Binding MfrSelection}" Margin="5,0" AutoGenerateColumns="False">
    ... 
                    <DataGridTemplateColumn x:Name="_PercentChange" IsReadOnly="True" Visibility="Visible" Header="Percent Change">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Margin="3,0" VerticalAlignment="Center">
                                    <TextBlock.Text>
                                        <MultiBinding Converter="{StaticResource VolumeToPercentConverter}">
                                            <Binding Path="YearVolume"/>
                                            <Binding Path="LastYearVolume"/>
                                        </MultiBinding>
                                    </TextBlock.Text>
                                </TextBlock>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

And the converter in my code behind:

public class VolumeToPercentConverter : IMultiValueConverter
{
    public object Convert(object[] value, Type targetType, object parameter, CultureInfo culture)
    {
        decimal percent = 0;
        if (value[0] is decimal && value[1] is decimal)
        {
            if ((decimal)value[1] != 0 && (decimal)value[0] != 0)
            {
                percent = ((decimal)value[0] - (decimal)value[1]) / (decimal)value[1];
                return percent;
            }
            else
            {
                return percent;
            }
        }
        else
        {
            return percent;
        }
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        return null;
    }
}

Upvotes: 1

Views: 1684

Answers (1)

Rob Archibald
Rob Archibald

Reputation: 397

The VolumeToPercentConverter is returning a decimal value, but the TextBlock.Text property is expecting a string. This is the error I received in the Output window when I created a test project:

Value produced by BindingExpression is not valid for target property.; Value='1' MultiBindingExpression:target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

I resolved this by updating the xaml as follows:

<MultiBinding Converter="{StaticResource VolumeToPercentConverter}" StringFormat="{}{0:P}">
    <Binding Path="YearVolume"/>
    <Binding Path="LastYearVolume"/>
</MultiBinding>

The secret sauce is the StringFormat="{}{O:P}" which formats the decimal into a nice percentage.

Upvotes: 4

Related Questions