Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WPF > Buttons > RadSplitButton with ContextMenu in DropDownContent Binding not working

Answered RadSplitButton with ContextMenu in DropDownContent Binding not working

Feed from this thread
  • Posted on Dec 30, 2011 (permalink)

    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

    Reply

  • Petar Mladenov Petar Mladenov admin's avatar

    Posted on Jan 4, 2012 (permalink)

    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 >>

    Reply

  • Say Hello to Telerik's PivotGrid for ASP.NET AJAX, Silverlight, WPF and WinForms. Now packed with OLAP support.
  • Posted on Jan 4, 2012 (permalink)

    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

    Reply

  • Answer Petar Mladenov Petar Mladenov admin's avatar

    Posted on Jan 9, 2012 (permalink)

    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 >>

    Attached files

    Reply

  • Jeff avatar

    Posted on Jan 8, 2013 (permalink)

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

    Reply

  • Tina Stancheva Tina Stancheva admin's avatar

    Posted on Jan 11, 2013 (permalink)

    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.

    Reply

  • Say Hello to Telerik's PivotGrid for ASP.NET AJAX, Silverlight, WPF and WinForms. Now packed with OLAP support.

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WPF > Buttons > RadSplitButton with ContextMenu in DropDownContent Binding not working