Posted 15 years ago
by Andy Ver Murlen
Version: 9.1.0500
Platform: .NET 3.5
Environment: Windows XP (32-bit)
Hi, I have noticed that when a ribbon button has a binding to it's own "IsEnabled" state, the binding does not properly update when "IsEnabled" changes. The most obvious use for this would be to hide/show the button when the command assigned to the button changes it's ability to execute. I am including some code to reproduce the problem. If you run the code and click the "toggle command" button, you will see that the standard WPF button toggle between visible and hidden. However, the ribbon button remains hidden the entire time. Note, that I am using the ribbon button outside of a ribbon for theme consistency.
<Window
x:Class="DockingSample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sample="clr-namespace:DockingSample"
xmlns:ribbon="clr-namespace:ActiproSoftware.Windows.Controls.Ribbon.Controls;assembly=ActiproSoftware.Ribbon.Wpf30"
Title="Window1"
WindowState="Maximized"
>
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
</Window.Resources>
<Window.CommandBindings>
<CommandBinding Command="sample:Window1.MyCommand" CanExecute="CommandBinding_CanExecute" Executed="CommandBinding_Executed" />
</Window.CommandBindings>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button
Margin="3"
Content="Toggle Command State"
Click="Button_Click"
/>
<ribbon:Button
Margin="3"
Grid.Row="1"
Label="Copy"
Command="sample:Window1.MyCommand"
Visibility="{Binding RelativeSource={RelativeSource Self}, Path=IsEnabled, Converter={StaticResource BooleanToVisibilityConverter}}"
/>
<Button
Margin="3"
Grid.Row="2"
Content="Copy"
Command="sample:Window1.MyCommand"
Visibility="{Binding RelativeSource={RelativeSource Self}, Path=IsEnabled, Converter={StaticResource BooleanToVisibilityConverter}}"
/>
</Grid>
</Window>
using System.Windows;
using System.Windows.Input;
namespace DockingSample
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1
{
// Fields
private bool _allowExecute;
// Constructors
public Window1()
{
InitializeComponent();
}
static Window1()
{
MyCommand = new RoutedUICommand("MyCommand", "MyCommand", typeof(Window1));
}
// Properties
public static RoutedUICommand MyCommand { get; private set; }
// Event Handlers
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
_allowExecute = !_allowExecute;
}
private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = _allowExecute;
}
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Command Executed");
}
}
}