Using Button in DataTemplate, binding Command & Label

Ribbon for WPF Forum

Posted 16 years ago by Phil Devaney - Senior Software Engineer, Serck Controls Ltd
Version: 4.5.0483
Avatar
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:

<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; }
    }
  }
}
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

Comments (2)

Posted 16 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Phil,

Can you send this over via email to us as a simple sample project that we can debug? Thanks!


Actipro Software Support

Posted 16 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Phil,

Thanks for reporting this and supplying the sample project. We've altered our internal code to implement command UI providers a bit different and the change resolves the issue. The maintenance release with the updates was just posted.


Actipro Software Support

The latest build of this product (v25.1.0) was released 29 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.