Multiple Column Data Template with Different Binding

6 posts, 1 answers
  1. Ryan Overton
    Ryan Overton avatar
    12 posts
    Member since:
    Oct 2009

    Posted 06 Jan 2010 Link to this post

    My problem is that I am wanting to use 1 data template for multiple colums in a gridview, but I want to bind the element in the data template to a different path.

    In the example below I have a template that creates a checkbox.

    <Grid.Resources> 
                <Style x:Key="CheckboxCellStyle" TargetType="{x:Type telerik:GridViewCell}">  
                    <Setter Property="ContentTemplate">  
                        <Setter.Value> 
                            <DataTemplate> 
                                <CheckBox VerticalAlignment="Center" IsChecked="{Binding ????}" Click="CheckBox_Click" /> 
                            </DataTemplate> 
                        </Setter.Value> 
                    </Setter> 
                </Style> 
    </Grid.Resources> 

    I want to bind the IsChecked property to the binded element in the data colulmn definition.

    <telerik:GridViewDataColumn Header="Deleted" DataMemberBinding="{Binding Deleted}" CellStyle="{StaticResource CheckboxCellStyle}"></telerik:GridViewDataColumn> 
    <telerik:GridViewDataColumn Header="Selected" DataMemberBinding="{Binding Selected}" CellStyle="{StaticResource CheckboxCellStyle}"></telerik:GridViewDataColumn> 


    Can someone please help me find a solution to this problem?
  2. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 08 Jan 2010 Link to this post

    Hi Ryan ,

    How about using an IValueConverter for the binding  and passing the column name as the converter parameter  ?

    Let me know if you need a working example on that .

    Greetings,
    Pavel Pavlov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Ryan Overton
    Ryan Overton avatar
    12 posts
    Member since:
    Oct 2009

    Posted 08 Jan 2010 Link to this post

    Hello Pavel,

    A working example would be greatly appreciated.

    Thank you,
    Ryan Overton
  5. Answer
    Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 12 Jan 2010 Link to this post

    Hello Ryan Overton,

    As promised I am attaching a small example. While playing with your scenario I found a way to do this even without converter.
    Hope it works for you!

    Best wishes,
    Pavel Pavlov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  6. Eric Belanger
    Eric Belanger avatar
    14 posts
    Member since:
    Nov 2009

    Posted 02 Feb 2010 Link to this post

    The problem with the sample you provided is that the TwoWay binding mode is not working.  I changed the OneWay binding to TwoWay and when i change a cell value, the value doesn't push back the value to the underlying model.

    Is it something that is possible to do?
  7. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 03 Feb 2010 Link to this post

    Hello Eric Belanger,

    Yes indeed , messing with the cell value  overrides the default binding logic of the cell. Unfortunately I can not think of a way to do this without explicitly specifying the property  to be edited either in XAML or in code.

    There is a hacky solution - to handle to the click event of the checkbox and update the underlying business object in the event handler . However this would require reflection in the handler to obtain the property to be updated and set the value.  It becomes complicated and I am not sure it is a good way at all.

    Best wishes,
    Pavel Pavlov
    the Telerik team

    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
    Follow the status of features or bugs in PITS and vote for them to affect their priority.
Back to Top
UI for WPF is Visual Studio 2017 Ready