Search code examples
wpfxamlteleriktelerik-grid

How to bind booleans to DataTemplate


I have this DataTemplate for my RadGridView with a binding:

<DataTemplate x:Key="StatusTemplate">
    <local:StatusIndicator Style="{StaticResource StatusIndicator}" IsEnabled="{Binding}" />
</DataTemplate>

I'm using it on my GridViewDataColumn like this, where I bind booleans from my ItemsSource:

<telerik:GridViewDataColumn DataMemberBinding="{Binding Enabled}" CellTemplate="{StaticResource StatusTemplate}" />
<telerik:GridViewDataColumn DataMemberBinding="{Binding Connected}" CellTemplate="{StaticResource StatusTemplate}" />

But the Enabled and Connected properties assigned to the DataMemberBinding is not working,


Solution

  • I am afraid you won't be able to use the same DataTemplate for both columns. The binding path cannot be "injected" into the template in XAML which means that will need to create a unique DataTemplate with a specific binding path for each column:

    <telerik:GridViewDataColumn DataMemberBinding="{Binding Enabled}">
        <telerik:GridViewDataColumn.CellTemplate>
            <DataTemplate>
                <local:StatusIndicator Style="{StaticResource StatusIndicator}" IsEnabled="{Binding Enabled}" />
            </DataTemplate>
        </telerik:GridViewDataColumn.CellTemplate>
    </telerik:GridViewDataColumn>
    <telerik:GridViewDataColumn DataMemberBinding="{Binding Connected}">
        <telerik:GridViewDataColumn.CellTemplate>
            <DataTemplate>
                <local:StatusIndicator Style="{StaticResource StatusIndicator}" IsEnabled="{Binding Connected}" />
            </DataTemplate>
        </telerik:GridViewDataColumn.CellTemplate>
    </telerik:GridViewDataColumn>
    

    If the DataTemplate is more complicated, you might consider creating the templates and replace the binding path programmatically:

    Combining DataTemplates at runtime