Skip to content

Files

Latest commit

Nov 19, 2020
474d368 · Nov 19, 2020

History

History
83 lines (68 loc) · 2.64 KB

kb-buttons-splitbutton-howto-bind-command-of-button-part.md

File metadata and controls

83 lines (68 loc) · 2.64 KB
title description type page_title slug position tags ticketid res_type
How to set the Command property of the button part
Disable only the button part when the CanExecute of the command returns false.
how-to
Enable the dropdown part when the CanExecute of the Command is False
kb-buttons-splitbutton-howto-bind-command-of-button-part
0
splitbutton, command, button, canexecute, disabled
1085596
kb

Environment

Product Version 2019.2.618
Product RadButtons for WPF

Description

How to bind the Command property of the RadButton element(the left part) inside the RadSplitButton.

Solution

Handle the Loaded event of the RadSplitButton, use the [ChildrenOfType]({%slug common-visual-tree-helpers%}) extension method in order to locate its RadButton child and set its Command property.

[C#] Example 1: Setting the Command of the RadButton inside the RadSplitButton

{{region cs-kb-buttons-splitbutton-howto-bind-command-of-button-part-0}}

private void RadSplitButton_Loaded(object sender, RoutedEventArgs e)
{
    var splitButton = (RadSplitButton)sender;
    var buttonPart = splitButton.ChildrenOfType<RadButton>().FirstOrDefault(x => x.Name == "ButtonPart");
    if (buttonPart != null)
    {
        buttonPart.Command = new DelegateCommand(OnExecute, OnCanExecute);
    }
}

private bool OnCanExecute(object obj)
{
    // If this method returns false, only the button part of the RadSplitButton will be disabled
    return true;
}

private void OnExecute(object obj)
{
    MessageBox.Show("Button clicked!");
}

{{endregion}}

[VB.NET] Example 1: Setting the Command of the RadButton inside the RadSplitButton

{{region vb-kb-buttons-splitbutton-howto-bind-command-of-button-part-1}}

Private Sub RadSplitButton_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
	Dim splitButton = DirectCast(sender, RadSplitButton)
	Dim buttonPart = splitButton.ChildrenOfType(Of RadButton)().FirstOrDefault(Function(x) x.Name = "ButtonPart")
	If buttonPart IsNot Nothing Then
		buttonPart.Command = New DelegateCommand(AddressOf OnExecute, AddressOf OnCanExecute)
	End If
End Sub

Private Function OnCanExecute(ByVal obj As Object) As Boolean
    ' If this method returns False, only the button part of the RadSplitButton will be disabled
	Return True
End Function

Private Sub OnExecute(ByVal obj As Object)
	MessageBox.Show("Button clicked!")
End Sub

{{endregion}}

See Also

  • [DelegateCommand]({%slug common-mvvm-delegate-command-implementation%})