1

I would like to display "Yes" or "No" for whenever a Boolean type data received(it can receive different types of data) for generating a column in RadGridView instead of a checkbox. I would like to implement this changes in xaml. Columns are generating dynamically. This is how it's created now:

 <telerik:RadGridView x:Name="Data" Grid.Row="3" Margin="5" AutoGenerateColumns="False" CanUserSortColumns="True" IsFilteringAllowed="True"
                                 grid:RadGridViewColumnsBinding.ColumnsCollection="{Binding Path=ColumnsData}"
                                 IsReadOnly="False" CanUserResizeColumns="True"/>

I am new in Silverlight coding. Will really appreciate if someone can help.

MSIslam
  • 4,587
  • 6
  • 25
  • 28

1 Answers1

1

You should check out Telerik's ConditionalDataTemplateSelector they have in this demo, and read about IValueConverter if you haven't already.

Depending on what you are trying to do with all your columns, the ConditionalDataTemplateSelector might be overkill, but you can use it to create a rule system for what DataTemplate to use for a given cell based on a custom rule system.

<Grid.Resources>
    ...
    <DataTemplate x:Key="CellDisplayTextBox">
        <TextBlock Text="{Binding Value, Converter={StaticResource BooleanToYesNoConverter}}" />
    </DataTemplate>

    <selector:ConditionalDataTemplateSelector x:Key="displaySelector" ConditionConverter="{StaticResource someConverter}">
        <selector:ConditionalDataTemplateSelector.Rules>
            <selector:ConditionalDataTemplateRule DataTemplate="{StaticResource CellDisplayTextBox}">
                <selector:ConditionalDataTemplateRule.Value>
                    <sys:Int32>1</sys:Int32> <!--You need to figure out what value and type to use here -->
                </selector:ConditionalDataTemplateRule.Value>
            </selector:ConditionalDataTemplateRule>
            ...
    </selector:ConditionalDataTemplateSelector.Rules>
</Grid.Resources>
...
<telerikGridView:RadGridView>
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn CellTemplateSelector="{StaticResource displaySelector}" CellEditTemplateSelector="{StaticResource editSelector}" />
    </telerik:RadGridView.Columns>
</telerikGridView:RadGridView>

The IValueConverter will let you bind a bool value, but display a string value. For a BooleanToYesNoConverter you could do something like:

public class BooleanToYesNoConverter: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool? bValue = value as bool?;

        if (bValue.HasValue)
            return bValue.Value ? "Yes" : "No";
        else
            return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string sValue = value as string;
        return sValue == "Yes";
    }
}

The ConditionalDataTemplateSelector code from the demo:

public class ConditionalDataTemplateSelector : DataTemplateSelector
{
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        object conditionValue = this.ConditionConverter.Convert(item, null, null, null);
        foreach (ConditionalDataTemplateRule rule in this.Rules)
        {
            if (Equals(rule.Value, conditionValue))
            {
                return rule.DataTemplate;
            }
        }

        return base.SelectTemplate(item, container);
    }

    List<ConditionalDataTemplateRule> _Rules;
    public List<ConditionalDataTemplateRule> Rules 
    {
        get
        {
            if (this._Rules == null)
            {
                this._Rules = new List<ConditionalDataTemplateRule>();
            }

            return this._Rules;
        }
    }

    IValueConverter _ConditionConverter;
    public IValueConverter ConditionConverter
    {
        get
        {
            return this._ConditionConverter;
        }
        set
        {
            this._ConditionConverter = value;
        }
    }
}

public class ConditionalDataTemplateRule
{
    object _Value;
    public object Value
    {
        get
        {
            return this._Value;
        }
        set
        {
            this._Value = value;
        }
    }

    DataTemplate _DataTemplate;
    public DataTemplate DataTemplate
    {
        get
        {
            return this._DataTemplate;
        }
        set
        {
            this._DataTemplate = value;
        }
    }
}
matt
  • 333
  • 2
  • 10
  • 23
  • Thanks Matthew. IValueConverter looks like a good idea. The Demo is not there anymore, so having some confusion on how to implement that. The source 'Data' contains different type objects(String, Boolean etc). – MSIslam Feb 23 '16 at 22:04
  • I'll add the code for the ConditionalDataTemplateSelector. The selector should give you a way handle to different types of data. – matt Feb 23 '16 at 22:06
  • I tried to make the changes as you mentioned. But looks like it's not working. Crashing at some point, though not throwing error. – MSIslam Feb 24 '16 at 16:57
  • Are you generating the UI columns programmatically? If so you might need to just specify the datatemplate as part of adding each column. The ConditionalDataTemplateSelector does seem to cause crashes when called outside the xaml. – matt Feb 24 '16 at 17:23
  • Yes UI Columns are generated programmatically – MSIslam Feb 24 '16 at 17:26
  • It's going to depend a lot on how you're adding them programmatically, but you should still be able to set a datatemplate for each column before adding it. You just might need a more complicated IValueConverter depending on how your grid data is generated. – matt Feb 25 '16 at 21:34
  • On a different note, currently in the checkbox, when none of data there in backend it shows a blue sign in the checkbox. (for 1 it shows as checked, for 0 shows as unchecked/blank). Any idea how this blue sign could be setup or how to get rid of the blue signfrom the Checkbox? – MSIslam Mar 03 '16 at 00:12