Posted 16 years ago
by Phil Devaney
-
Senior Software Engineer,
Serck Controls Ltd
Version: 4.5.0483
I am trying to use a Button in a DataTemplate and specify a binding on both the Command and Label properties, but if the command is a RibbonCommand with a label specified, the Command's label is displayed instead of the Button's label. If I bind both properties in a non-templated context, then the Button's label overrides the Command's label as expected.
Here's an example:
My actual scenario involves an ItemsControl, with multiple buttons using the same command but with different labels, but this is simpler. I also want to be able to use the same command elsewhere in the UI without overriding its label. Note that if the RibbonCommand doesn't have a label, then the Button label is displayed even when templated.
Phil
Here's an example:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:r="http://schemas.actiprosoftware.com/winfx/xaml/ribbon"
xmlns:local="clr-namespace:WpfApplication1"
Title="Window1" SizeToContent="WidthAndHeight">
<Window.CommandBindings>
<CommandBinding Command="local:Window1.FooCommand" Executed="FooCommand_Execute" />
</Window.CommandBindings>
<Window.Resources>
<DataTemplate DataType="{x:Type local:Foo}">
<r:Button Margin="5" Padding="5" HorizontalContentAlignment="Center"
Label="{Binding Name}" Command="{Binding Command}" />
</DataTemplate>
</Window.Resources>
<StackPanel>
<r:Button Margin="5" Padding="5" HorizontalContentAlignment="Center"
Label="{Binding Name}" Command="{Binding Command}" />
<ContentControl Content="{Binding}" />
</StackPanel>
</Window>
using System;
using System.Windows;
using System.Windows.Input;
using ActiproSoftware.Windows.Controls.Ribbon.Input;
namespace WpfApplication1
{
public partial class Window1 : Window
{
public Window1()
{
this.DataContext = new Foo() { Name = "Foo 1" };
this.InitializeComponent();
}
public static readonly RibbonCommand FooCommand =
new RibbonCommand( "FooCommand", typeof( Foo ), "Foo Command" );
private void FooCommand_Execute( object sender, ExecutedRoutedEventArgs e )
{
MessageBox.Show( "Foo Executed" );
}
}
public class Foo
{
public string Name { get; set; }
public RibbonCommand Command
{
get { return Window1.FooCommand; }
}
}
}
Phil