This is a migrated thread and some comments may be shown as answers.

Maintain selected value of RadCombox

5 Answers 274 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Jackey
Top achievements
Rank 1
Jackey asked on 17 Oct 2014, 02:29 PM
Hi guys,

I am try my best to make myself clear. I have two pages with one of them being Config page. In the config page, I have a RadCombox to choose a time span value. The code snippets are below.
<telerik:RadComboBox Width="125" FontSize="{DynamicResource FontSizeBig}"
        VerticalAlignment="Center" Height="32" Margin="0,0,5,0" SelectedValue="{Binding Realtime.TimeRemaining, Converter={StaticResource DetectionDurationConverter}, Source={StaticResource Locator}}">
              <telerik:RadComboBoxItem Content="10 s" />
              <telerik:RadComboBoxItem Content="10 minutes"/>
              <telerik:RadComboBoxItem Content="1 hour"/>
              <telerik:RadComboBoxItem Content="Custom" Margin="0"/>
</telerik:RadComboBox>

As you can see from the above code, I binds the selected value to TimeRemaing property and I would use a customized converter named DetectionDurationConverter to turn 10 s, 10 minutes and 1 hour into a time span value in C#.  Here is my converter. 
public class DetectionDurationConverter : IValueConverter
{
    private static readonly string[] durationSet = new[] {"10 s", "10 minutes", "1 hour"};
 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var timeSpan = (TimeSpan) value;
        if (timeSpan == TimeSpan.FromSeconds(10.0))
        {
            return durationSet[0];
        } else if (timeSpan == TimeSpan.FromMinutes(10.0))
        {
            return durationSet[1];
        } else if (timeSpan == TimeSpan.FromHours(1.0))
        {
            return durationSet[2];
        }
        return string.Empty;
    }
 
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var timeSpan = TimeSpan.MinValue;
        if (value is RadComboBoxItem)
        {
            var val = System.Convert.ToString((value as RadComboBoxItem).Content);
            if (val == durationSet[0])
            {
                timeSpan = TimeSpan.FromSeconds(10.0);
            }
            else if (val == durationSet[1])
            {
                timeSpan = TimeSpan.FromMinutes(10.0);
            }
            else if (val == durationSet[2])
            {
                timeSpan = TimeSpan.FromHours(1.0);
            }
        }
        return timeSpan;
    }
}

For example, when I select a '10 s', it goes to ConvertBack method as expected and TimeRemaining is a 10 second time span value. Then I switched to another page and switched back, it goes to Convert method with TimeRemaining being 10 seonds still. Note, the method Convert would be entered twice. However, in the UI, no items have been selected. I just hope the selected item would be consistent with the value of TimeRemaining.

Thanks,
-J

5 Answers, 1 is accepted

Sort by
0
Jackey
Top achievements
Rank 1
answered on 17 Oct 2014, 02:45 PM
Somebody helps me to move this thread to Combox forum. Thanks a lot!
0
Nasko
Telerik team
answered on 20 Oct 2014, 03:09 PM
Hello Jingfei,

What I can suggest you in order to achieve the desired functionality would be to bind the ItemsSource of the ComboBox to a collection of objects instead of using static RadComboBoxItems:

http://www.telerik.com/help/wpf/radcombobox-populating-with-data-binding-to-object.html

The reason why the converter does not work properly is because it returns objects of type string while it is expected to return RadComboBoxItem and thus in the UI no items would be selected.

I hope this information will help you.

Regards,
Nasko
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Jackey
Top achievements
Rank 1
answered on 25 Oct 2014, 09:41 AM
Hi Nasko,

Thanks very much for your reply. I would take your suggestion then.
For expanding my knowledge, I still want to know how to return a RadComboBoxItem object to make the converter work?  Following code also didn't work.

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    var boxItem = new RadComboBoxItem();
    var timeSpan = (TimeSpan) value;
    if (timeSpan == TimeSpan.FromSeconds(10.0))
    {
        boxItem.Content = durationSet[0];
    } else if (timeSpan == TimeSpan.FromMinutes(10.0))
    {
        boxItem.Content = durationSet[1];
    } else if (timeSpan == TimeSpan.FromHours(1.0))
    {
        boxItem.Content = durationSet[2];
    }
    return boxItem;
}

Thanks,
-J
0
Accepted
Nasko
Telerik team
answered on 27 Oct 2014, 02:36 PM
Hi Jingfei,

That implementation of the Convert method in this case could not work properly due to the fact that it should return the instance of the required item instead of a newly created RadComboBoxItem. However I recommend you to use the the approach I suggested you it the previous response.

Please do not hesitate to contact us if you have any additional questions.

Regards,
Nasko
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Jackey
Top achievements
Rank 1
answered on 30 Oct 2014, 01:47 AM
Thanks a lot, Nasko.

I know what I should do now. 
Tags
ComboBox
Asked by
Jackey
Top achievements
Rank 1
Answers by
Jackey
Top achievements
Rank 1
Nasko
Telerik team
Share this question
or