0

I have a DataGridTemplateColumn in a DataGrid that looks like this:

<DataGridTemplateColumn Width="3*" Header="Item">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding AssetDescriptionID} Converter={StaticResource converter}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <ComboBox
                DisplayMemberPath="Description"
                ItemsSource="{Binding Data.AssetDescriptions, Source={StaticResource proxy}}"
                SelectedValue="{Binding AssetDescriptionID}"
                SelectedValuePath="AssetDescriptionID" />
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

I would like to use a Multi-Value Converter to lookup an Asset Description from another table by the Asset Description ID, in this (probably wrong) line:

<TextBlock Text="{Binding AssetDescriptionID} Converter={StaticResource converter}" />

The ViewModel has a public property containing the Asset Descriptions:

public IEnumerable<AssetDescription> AssetDescriptions { get; set; }

Where AssetDescription is essentially:

public class AssetDescription
{
    public int AssetDescriptionID { get; set; }
    public string Description { get; set; }
}

So I've written this converter class:

public class AssetDescriptionConverter : FrameworkElement, IMultiValueConverter
{
    public IEnumerable<T_AssetDescription> AssetDescriptions
    {
        get { return (IEnumerable<T_AssetDescription>)GetValue(AssetDescriptionsProperty); }
        set { SetValue(AssetDescriptionsProperty, value); }
    }

    public static readonly DependencyProperty AssetDescriptionsProperty =
        DependencyProperty.Register("AssetDescriptions", typeof(IEnumerable<T_AssetDescription>), typeof(AssetDescriptionConverter));

    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        var items = values as IEnumerable<T_Asset>;
        if (items != null)
        var item = items.FirstOrDefault(x => x.AssetDescriptionID == (int)parameter);
        return item == null ? string.Empty : item.Description;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

But I can't figure out how to declare this resource in the XAML and then bind it to my TextBlock.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • Use a `` instead of a simple binding. One binding of the MultiBinding should provide the AssetDescriptionID, and another binding in the MultiBinding should provide the "other" table data. I am rather confused by your attempt at making a (multi) value converter a FrameworkElement, though. It looks like you either trying to do some hardcore tricky, dirty stuff; or you perhaps might have misunderstood the purpose of those value converters and how they are used by the binding mechanism... –  Nov 30 '18 at 16:03
  • @elgonzo: https://stackoverflow.com/questions/53549281/how-do-i-get-a-datagridtemplatecolumn-to-work-with-objects-implementing-the-iedi#comment93965305_53549281 – Robert Harvey Nov 30 '18 at 16:50
  • A IMultiValueConverter only works with a MultiBinding. This should be an IValueConverter. `{Binding AssetDescriptionID}` will not pass multiple values to a converter, but just a collection object to the single value of an IValueConverter. – Clemens Nov 30 '18 at 16:58
  • @Clemens: I figured as much. – Robert Harvey Nov 30 '18 at 17:22
  • I am not sure what the comment you linked to should tell me. Anyway, if it becomes too complicated attempting to bind between different controls because of (item-)data-templating or other issues, you might think about the idea to move this kind of data dependency you try to solve with control-to-control binding into the realm of viewmodels (basically, instead of trying to use a value converter, use dedicated viewmodels for the items(?) which then would do the data translation/conversion). –  Nov 30 '18 at 17:57

1 Answers1

-1

In the resource part:

<AssetDescriptionConverter x:Key="AssetDescriptionConverter"/>

and then in xaml:

<TextBlock Text="{Binding AssetDescriptionID, Converter={StaticResource AssetDescriptionConverter}}"/>
mami
  • 586
  • 1
  • 4
  • 14