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

RadSplitButton with ContextMenu in DropDownContent Binding not working

5 Answers 249 Views
Buttons
This is a migrated thread and some comments may be shown as answers.
Tony
Top achievements
Rank 1
Tony asked on 30 Dec 2011, 06:41 PM
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

5 Answers, 1 is accepted

Sort by
0
Petar Mladenov
Telerik team
answered on 04 Jan 2012, 12:24 PM
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 >>

0
Tony
Top achievements
Rank 1
answered on 04 Jan 2012, 04:12 PM
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
0
Accepted
Petar Mladenov
Telerik team
answered on 09 Jan 2012, 10:23 AM
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 >>

0
Jeff
Top achievements
Rank 1
answered on 08 Jan 2013, 06:35 PM
What if you want your ListBox items to look like menu items instead of ListBoxItems?
0
Tina Stancheva
Telerik team
answered on 11 Jan 2013, 09:29 AM
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.

Tags
Buttons
Asked by
Tony
Top achievements
Rank 1
Answers by
Petar Mladenov
Telerik team
Tony
Top achievements
Rank 1
Jeff
Top achievements
Rank 1
Tina Stancheva
Telerik team
Share this question
or