Maintain selected value of RadCombox

6 posts, 1 answers
  1. Jackey
    Jackey avatar
    71 posts
    Member since:
    Oct 2014

    Posted 17 Oct 2014 Link to this post

    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
  2. Jackey
    Jackey avatar
    71 posts
    Member since:
    Oct 2014

    Posted 17 Oct 2014 in reply to Jackey Link to this post

    Somebody helps me to move this thread to Combox forum. Thanks a lot!
  3. UI for WPF is Visual Studio 2017 Ready
  4. Nasko
    Admin
    Nasko avatar
    588 posts

    Posted 20 Oct 2014 Link to this post

    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.

     
  5. Jackey
    Jackey avatar
    71 posts
    Member since:
    Oct 2014

    Posted 25 Oct 2014 in reply to Nasko Link to this post

    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
  6. Answer
    Nasko
    Admin
    Nasko avatar
    588 posts

    Posted 27 Oct 2014 Link to this post

    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.

     
  7. Jackey
    Jackey avatar
    71 posts
    Member since:
    Oct 2014

    Posted 29 Oct 2014 in reply to Nasko Link to this post

    Thanks a lot, Nasko.

    I know what I should do now. 
Back to Top
UI for WPF is Visual Studio 2017 Ready