How to close RadWindow dialog in MVVM using custom button
Environment
Product Version | 2019.1.116 |
Product | RadWindow for WPF |
Description
How to close a RadWindow Confirm dialog in MVVM scenario using custom button.
Solution
- Create a custom style which targets RadConfirm (for example) dialog.
- Override the ControlTemplate by specifying your own one. You can add your button.
- Bind the command property of the button to your custom command using RelativeSource binding.
- To close the dialog in the executed method you need to pass the RadWindow to the CommandParameter using the same approach with RelativeSource binding.
<Style TargetType="telerik:RadConfirm">
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="MinWidth" Value="275"/>
<Setter Property="MaxWidth" Value="500"/>
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="telerik:RadConfirm">
<!--Your custom template-->
<telerik:RadButton Content=" My Button" Command="{Binding Content.MyCommand,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=telerik:RadConfirm}}" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=telerik:RadWindow,Mode=FindAncestor}}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
public class MainViewModel
{
public ICommand MyCommand { get; set; }
public MainViewModel()
{
MyCommand = new DelegateCommand(OnCommandExecuted);
}
private void OnCommandExecuted(object obj)
{
var window = obj as RadWindow;
window.Close();
}
}