Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If the only added functionality that you want for your button is to have an image on it, then I think you're approaching this from the wrong direction. WPF is as awesome as it is because the UI controls are <strong>look-less.</strong> This means that a Control is merely a definition of functionality + some template to define how it looks. This means that the template can be swapped out at any time to change the look. Also, almost any content can be placed inside of almost any control</p> <p>For instance, to define a button in your xaml that has the look your going for all you need is this:</p> <pre><code>&lt;Window ...&gt; ... &lt;Button Command="{Binding AttachContextCommand}"&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;Image Source="{StaticResource AssociateImage}"/&gt; &lt;TextBlock Text="Associer"/&gt; &lt;/StackPanel&gt; &lt;/Button&gt; ... &lt;/Window&gt; </code></pre> <p>Just keep in mind that with WPF you don't have to define a new CustomControl or UserControl every time you want to change the look and feel of something. The only time you should need a CustomControl is if you want to add functionality to an existing Control or to create functionality that doesn't exist in any other Control.</p> <p><strong>Edit Due to comment:</strong></p> <p>If you're wanting to keep from defining the content for the button every time, the other option is to just have a poco (plain old CLR object) class that would define everything your interested in (I'll write my example as if you're doing this for a tool bar, because it makes sense to me):</p> <pre><code>public class ToolBarItem : INotifyPropertyChanged { public string Text { get ... set ... } public ICommand Command { get ... set ... } public ImageSource Image { get ... set ... } } </code></pre> <p>That has a data template defined somewhere (App.xaml, Window.Resources, etc):</p> <pre><code>&lt;DataTemplate DataType="{x:Type l:ToolBarItem}"&gt; &lt;Button Command="{Binding Command}"&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;Image Source="{Binding Image}"/&gt; &lt;TextBlock Text="{Binding Text}"/&gt; &lt;/StackPanel&gt; &lt;/Button&gt; &lt;/DataTemplate&gt; </code></pre> <p>And then use the guy in your xaml like this:</p> <pre><code>&lt;Window ...&gt; ... &lt;ContentControl&gt; &lt;ContentControl.Content&gt; &lt;l:ToolBarItem Image="..." Command="..." Text="..."/&gt; &lt;/ContentControl.Content&gt; &lt;/ContentControl&gt; ... &lt;/Window&gt; </code></pre> <p>I just don't know that the way you're trying to do it is the most WPF way you could do it.</p> <p><strong>EDIT Updated based on second comment</strong> Sorry, I forgot to include the ContentControl surrounding that. Now that I remembered that, I realize that that's not much less verbose than the original where you are specifying the content manually. I'll post a new answer to help with your original question.</p>
 

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