Note that there are some explanatory texts on larger screens.

plurals
  1. POVariable Image Source inside a Button Template
    primarykey
    data
    text
    <p>How can I transfer the Source attribute from a button template to an Image inside that template ?</p> <p>This is how my XAML looks like:</p> <pre><code>&lt;Page x:Class="TallShip.MainGallery" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:TallShip" mc:Ignorable="d" d:DesignHeight="1080" d:DesignWidth="1920" Title="MainGallery"&gt; &lt;Grid&gt; &lt;ScrollViewer Height="958" HorizontalAlignment="Left" Margin="685,87,0,0" Name="GalleryRightScroller" VerticalAlignment="Top" Width="1159" PanningMode="VerticalOnly" VerticalScrollBarVisibility="Auto"&gt; &lt;Grid&gt; &lt;Grid.Resources&gt; &lt;ControlTemplate x:Key="GalleryObject"&gt; &lt;StackPanel Height="Auto" HorizontalAlignment="Left" Margin="{TemplateBinding Margin}" VerticalAlignment="Top" Width="500"&gt; &lt;Image Height="200" Stretch="None" Source="{TemplateBinding local:SourceHolder.Source}" Width="200" /&gt; &lt;Label Content="{TemplateBinding local:SourceHolder.Source}" Height="28" Name="label1" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" /&gt; &lt;/StackPanel&gt; &lt;/ControlTemplate&gt; &lt;/Grid.Resources&gt; &lt;Button Template="{StaticResource GalleryObject}" local:SourceHolder.Source="/TallShip;component/media/91gx2EQ.jpg" Name="object1" Margin="43,21,0,0"/&gt; &lt;Button Template="{StaticResource GalleryObject}" local:SourceHolder.Source="/TallShip;component/media/91gx2EQ.jpg" Name="object2" Margin="43,280,0,0"/&gt; &lt;/Grid&gt; &lt;/ScrollViewer&gt; &lt;/Grid&gt; </code></pre> <p></p> <p>And this is the custom control class I've created to hold the Source property:</p> <pre><code>namespace TallShip { public static class SourceHolder { public static readonly DependencyProperty SourceProperty = DependencyProperty.RegisterAttached("Source", typeof(string), typeof(SourceHolder), new FrameworkPropertyMetadata(null)); public static string GetSource(UIElement element) { if (element == null) throw new ArgumentNullException("element"); return (string)element.GetValue(SourceProperty); } public static void SetSource(UIElement element, string value) { if (element == null) throw new ArgumentNullException("element"); element.SetValue(SourceProperty, value); } } } </code></pre> <p>Currently, the label correctly displays the passed source, but the image does not work...</p> <p>If I use the same source on an image outside of the button template, the image works just fine.</p>
    singulars
    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.
    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