Binding to the current object or to its GetType method

Answered Binding to the current object or to its GetType method

  • mercredi 24 juin 2009 15:13
     
     
     Hello,

    I have a collection of objects which is displayed by a component (the WPF Toolkit DataGrid).
    Some properties of these objects are directly displayed but a column must contain a string derived from each object's type.

    So the first solution would be to bind the column to the object itself and to add a value converter that would produce the good string.
    1) How to bind directly to the object and not to one of its properties ?

    Another solution would be to bind to the "GetType" method instead of a property and also add a converter that would convert the result of "GetType" to the good format.
    2) Is it possible to bind to a method ?

    Thanks by advance.

Toutes les réponses

  • mercredi 24 juin 2009 15:14
     
     Traitée
    Just call {Binding}.  Don't use {Binding PropertyName} or {Binding Path=PropertyName}.  Just {Binding}.
    David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF MSDN Forums Client
    • Marqué comme réponse seriousme mercredi 24 juin 2009 16:40
    •  
  • mercredi 24 juin 2009 16:42
     
     Traitée A du code
    Thanks a lot, that's perfect.

    <UserControl x:Class="Test.Control1"
    	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    	xmlns:wpftk="http://schemas.microsoft.com/wpf/2008/toolkit" 
    	xmlns:local="clr-namespace:Test" 
    	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    	<UserControl.Resources>
    		<local:Converter x:Key="converter" />
    	</UserControl.Resources>
    	<wpftk:DataGrid
    	  x:Name="grid"
    	  ItemsSource="{Binding Path=Items}"
    	  IsReadOnly="True"
    	  AutoGenerateColumns="False">
    		<wpftk:DataGrid.Columns>
    			<wpftk:DataGridTextColumn Header="Field1" Binding="{Binding Path=Field1}" Width="Auto" />
    			<wpftk:DataGridTextColumn Header="Field2" Width="Auto" Binding="{Binding Converter={StaticResource converter}}" />
    		</wpftk:DataGrid.Columns>
    	</wpftk:DataGrid>
    </UserControl>
    
    • Marqué comme réponse seriousme mercredi 24 juin 2009 16:42
    •  
  • vendredi 3 février 2012 09:14
     
     
    You are right in most of the cases. But this won't work if the ToString()  method of the bound object has been overriden.
  • mercredi 22 août 2012 09:05
     
     

    Hello David,

    How to bind to "GetType().Name"?????

    Regards

    Salam


    salampv.07