char m
char m

Reputation: 8336

How to bind to WPF DataGrid row class instance and not it's property?

I need to decide an icon for a DataGrid-column for a row and thought I let a converter do it. Converter would just get the whole row and then decide by multiple properties which xaml-geometry to return

 <DataGrid CanUserAddRows="False" CanUserResizeColumns="True" CanUserSortColumns="True" IsReadOnly="True" 
               ItemsSource="{Binding SelectedObjectsMmsDataItems}">
        <DataGrid.Resources>
            <DataTemplate x:Key="TypeImageColumnTemplate" >
                <Label Style="{StaticResource DataGridIconColumnLabel}">
                    <Path Data="{Binding???, Converter={StaticResource MmsDataToPathConverter}}" />
                </Label>
            </DataTemplate>
           <DataGrid.Columns>
           <DataGridTemplateColumn Header="{StaticResource ResourceKey=StrIcon}" CellTemplate="{StaticResource TypeImageColumnTemplate}" Width="Auto"/>

How to do this?

Upvotes: 1

Views: 1835

Answers (3)

AnjumSKhan
AnjumSKhan

Reputation: 9827

This will give you the DataGridRow

<Path Data="{Binding ., RelativeSource={RelativeSource AncestorType=DataGridRow}, Converter={StaticResource MmsDataToPathConverter}}" />

This will give you the Item (eg; Employee) presented by that row.

<Path Data="{Binding DataContext, RelativeSource={RelativeSource AncestorType=DataGridRow}, Converter={StaticResource MmsDataToPathConverter}}" />

Upvotes: 3

char m
char m

Reputation: 8336

This is done with following XAML. When there's only word Binding without anything else, comma should be left out before Converter.

 <Path Data="{Binding Converter={StaticResource MmsDataToPathConverter}}"       

Upvotes: 0

DRapp
DRapp

Reputation: 48139

If the structure of your binding is that of an IEnumerable or Collection such as a class structure, you should be able to do by exposing your own custom property and doing all the manipulation of it from there. Something like

public class YourClass
{
   public YourClass()
   {...}

   public int SomeField { get; set;}
   public string OtherField {get; set;}
   ...

   public whateverDataType YourNewPropertyToBindTo
   {
      get { if( SomeField == 1 && OtherField == "Test" )
               return "X";

            if( SomeField == 2  && OtherFIeld == "SomethingElse"
               return "Y";

            return "Z"; // as a default return value. }
   }
}

Then, you can access all the properties and proper data types directly from your record / class structure source. Hope this option helps.

Upvotes: 0

Related Questions