0
votes

I've got a DataGrid that Auto Generates it's columns.

In Code i implement The AutoGeneratingColumn Event, to set a certain template for my Translation Datatype:

    private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {
        if(e.PropertyType == typeof(Translation)){
            DataGridTemplateColumn templateColumn = new DataGridTemplateColumn();
            templateColumn.CellTemplate = (DataTemplate)Resources["LanguageTemplate"];
            e.Column = templateColumn;
        }

    }

DataTemplate:

    <DataTemplate x:Key="LanguageTemplate">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="20"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <TextBlock Text="{Binding Name.ActualTranslation}" HorizontalAlignment="Stretch" Grid.Column="0"></TextBlock>
            <Image Source="{lex:LocImage en}" Height="15" HorizontalAlignment="Right" Grid.Column="1" Visibility="{Binding Name.HasCurrentLanguage, Converter={StaticResource boolToVis}, ConverterParameter=true}" ></Image>
        </Grid>

    </DataTemplate>

Now a problem occured: The TextBlock is bound to Name Property. That works fine if the object to be displayed has a Name Property. But if i have Translation properties that are not named "Name" obviously no data is shown. How would i bind correctly to cover all Translation Items.

2
Could you also post the Translation-Class? - Florian Gl

2 Answers

0
votes

Create a DataTemplateSelector where can choose the right DataTemplate for your current Translation class and add it to your TextBlock.Text binding. See more

0
votes

As it looks like a one way binding, do a simple binding with converter :

TextBlock Text="{Binding Converter={StaticResource your_converter}}" ...

and implement your logic in the converter. It's the simpliest way if you have inconsistent ViewModels you want to bind on.