RadSplitButton with ContextMenu in DropDownContent Binding not working

6 posts, 1 answers
  1. Tony
    Tony avatar
    95 posts
    Member since:
    May 2011

    Posted 30 Dec 2011 Link to this post

    I have a RadSplitButton on my form.  The RadSplitButton's DropDownContent contains a RadContextMenu.  Here's a snippet of the xaml:

    <telerik:RadSplitButton Background="{DynamicResource ButtonBackground}"
                            Click="MisreadButton_Click"
                            CloseOnEscape="False"
                            Content="INCORRECT"
                            DropDownIndicatorVisibility="Visible"
                            DropDownPlacement="Top"
                            FontSize="20"
                            FontWeight="Bold"
                            Foreground="{DynamicResource ButtonForeground}"
                            Height="60"
                            HorizontalAlignment="Right"
                            IsEnabled="False"
                            Margin="10"
                            Name="IncorrectButton"
                            VerticalAlignment="Center"
                            Width="200">
        <telerik:RadSplitButton.DropDownContent>
            <tl:RadContextMenu DisplayMemberPath="Value"
                               ItemsSource="{Binding Path=RejectionReasons, RelativeSource={RelativeSource AncestorType={x:Type c:AlarmsDialog}}}"
                               tl:RadMenuItem.Click="RadContextMenu_ItemClick" />
            </telerik:RadSplitButton.DropDownContent>
    </telerik:RadSplitButton>
    In the Window's constructor, after calling InitializeComponent, I create an ObservableCollection and assign it to RejectionReasons.  I then populate that ObservableCollection with ItemChoice objects:

    public class ItemChoice<TKey> : INotifyPropertyChanged {
          
        private TKey iKey;
        public TKey Key {
            get { return iKey; }
            set {
                iKey = value;
                if ( PropertyChanged != null ) {
                    PropertyChangedEventArgs e = new PropertyChangedEventArgs( "Key" );
                    PropertyChanged( this, e );
                }
            }
        }
      
        private string iValue = string.Empty;
        public string Value {
            get { return iValue; }
            set {
                iValue = value;
                if ( PropertyChanged != null ) {
                    PropertyChangedEventArgs e = new PropertyChangedEventArgs( "Value" );
                    PropertyChanged( this, e );
                }
            }
        }
      
        public ItemChoice() {}
      
        public ItemChoice( TKey key, string value ) {
            Key = key;
            Value = value;
        }
      
        public ItemChoice( KeyValuePair<TKey, string> item ) {
            Key = item.Key;
            Value = item.Value;
        }
      
        public override string ToString() {
            return Value.ToString();
        }
      
        public static explicit operator KeyValuePair<TKey, string>( ItemChoice<TKey> item ) {
            return new KeyValuePair<TKey, string>( item.Key, item.Value );
        }
      
        public event PropertyChangedEventHandler  PropertyChanged;
    }
     
    But the RadContextMenu's Items collection remains empty, even after I populate the collection.  I use this same ItemChoice class with ComboBoxes and ListBoxes and it works fine.  What am I doing wrong?

    Also, in my RadMenuItem.Click handler, how do I get a reference to the ItemChoice that generated the RadMenuItem?  It's not at all clear to me from the properties that are available in that class.

    Tony

  2. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2528 posts
    Member since:
    Sep 2012

    Posted 04 Jan 2012 Link to this post

    Hi Tony,

     First of all, I would like to point out that using RadContextMenu in a DropDownContent of a DropDownButton is not good practice. Many issues may occur in such situations because both the ContextMenu and the DropDownContent are Popups.
    The reason your RadContextMenu is empty is that it is located in Popup (the DropDownContent) and therefore it participates in different visual tree than the one that holds the DropDownButton (and the "AlarmsDialog") . This way the Binding to RelativeSource cannot work, it does't find the desired relative source since the ContextMenu and the AlarmsDialog are in different visual trees.

    Regards,
    Petar Mladenov
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. Tony
    Tony avatar
    95 posts
    Member since:
    May 2011

    Posted 04 Jan 2012 Link to this post

    Peter:

    OK, I understand what you're saying. 

    So what should I use instead?  I want the user to pick from a list of choices when they click on the drop down portion of the RadSplitButton, much like a ComboBox.  And I want to populate those choices from a database query.

    Thanks

    Tony

  4. Answer
    Petar Mladenov
    Admin
    Petar Mladenov avatar
    2528 posts
    Member since:
    Sep 2012

    Posted 09 Jan 2012 Link to this post

    Hi Tony,

     You can consider using a MainViewModel in your MainPage and bind the ItemsSource of the ListBox inside your DropDownContent to a property from the ViewModel. This VM could represent classes that wrap the business objects from the database. Is this suitable for you?

    All the best,
    Petar Mladenov
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  5. Jeff
    Jeff avatar
    25 posts
    Member since:
    Jul 2012

    Posted 08 Jan 2013 Link to this post

    What if you want your ListBox items to look like menu items instead of ListBoxItems?

  6. Tina Stancheva
    Admin
    Tina Stancheva avatar
    2879 posts
    Member since:
    Apr 2014

    Posted 11 Jan 2013 Link to this post

    Hi Jeff,

    When you want to create a Button with a  DropDown ContextMenu, we recommend following the approach described here.

    However, if you need to use a RadSplitButton in particular, then you can define a RadMenu control in its DropDownContent. Then you can implement a custom style for the RadMenu to make it look like the default style of the RadContextMenu control. I attached a sample solution demonstrating this approach in case you need to use it in your application.

    All the best,
    Tina Stancheva
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top