Note that there are some explanatory texts on larger screens.

plurals
  1. PODatabinding to Command in Silverlight Templated Button control
    primarykey
    data
    text
    <p>I am trying to create a templated button control with databinding for the Visibility, tooltip, and Command. The Visibility binding works, as does the tooltip, but the Command does not. Another process is responsible for injecting the viewmodel and associating it with the View, and the other data bindings are working so I am pretty confident that is working properly.</p> <p>In the resource dictionary:</p> <pre><code>&lt;Converters:BoolToVisibilityConverter x:Key="boolVisibilityConverter" /&gt; &lt;Style TargetType="local:ImageButton"&gt; &lt;Setter Property="Visibility" Value="{Binding FallbackValue=Visible, Path=ToolIsAvailable, Converter={StaticResource boolVisibilityConverter} }"/&gt; &lt;Setter Property="Command" Value="{Binding ButtonCommand}"/&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="local:ImageButton"&gt; &lt;Grid&gt; &lt;Image Source="{TemplateBinding Image}" ToolTipService.ToolTip="{Binding ToolName}" /&gt; &lt;/Grid&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; </code></pre> <p>the templated control</p> <pre><code>public class MyButton: ImageButton { public MyButton(MyCommandViewModel viewmodel) { this.DefaultStyleKey = typeof(ImageButton); this.Image = new BitmapImage(new Uri("/MyProject;component/Themes/myimage.png", UriKind.Relative)); this.DataContext = viewmodel; } } </code></pre> <p>and in the view model</p> <pre><code>public MyCommandViewModel() : base("My Tool", true) { } public class CommandViewModel { public CommandViewModel(string toolName, bool isAvailable) { ToolIsAvailable = isAvailable; ToolName = toolName; _buttoncommand = new DelegateCommand(() =&gt; { ExecuteCommand(); }, () =&gt; { return CanExecute; }); } private bool _canExecute = true; public bool CanExecute { get { return _canExecute; } set { _canExecute = value; OnPropertyChanged("CanExecute"); if (_command != null) _command.RaiseCanExecuteChanged(); } } private DelegateCommand _buttoncommand; public ICommand ButtonCommand { get { return _buttoncommand; } } protected virtual void ExecuteCommand() { } public bool ToolIsAvailable { get { return _toolIsReady; } set { _toolIsReady = value; OnPropertyChanged("ToolIsAvailable"); } } public string ToolName { get { return _toolName; } set { _toolName = value; OnPropertyChanged("ToolName"); } } } </code></pre> <p>Why are the other databindings functioning properly but not the Command data binding. I found this similar post <a href="https://stackoverflow.com/questions/2088876/overriding-a-templated-buttons-command-in-wpf">Overriding a templated Button&#39;s Command in WPF</a> Do I need to template a grid control instead and use RoutedCommands? I am not sure I understand why Silverlight treats the Command binding different than the others so I suspect I just have a bug in the code.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload