Why am I having a problem snapping to an image from a control template?

I have an item control style that binds to an image in the target, it only appears if the target is also bind to the image, and I have no idea why .. can anyone throw light on this for me ?

A simplified version of my style:

<Style x:Key="testStyle" TargetType="ItemsControl">
    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate>
                <DockPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto" Height="Auto" LastChildFill="True">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="32"/>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>
                            <Grid Grid.Row="0" DockPanel.Dock="Top" MinHeight="25" SnapsToDevicePixels="True">
                                <StackPanel Orientation="Horizontal">
                                    <Image Margin="10,0,10,0" VerticalAlignment="Stretch" Height="24" Width="24"  Source="{Binding Path=HeaderImage}" />
                                    <TextBlock FontFamily="Tahoma" VerticalAlignment="Center" Text="{Binding Path=HeaderInfo}" />
                                </StackPanel>
                                <Line VerticalAlignment="Bottom" Stretch="Fill"/>
                            </Grid>
                            <ItemsPresenter Grid.Row="1"/>
                        </Grid>
                    </DockPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

      

My user control:

<UserControl x:Class="StartPageView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<ItemsControl Style="{DynamicResource testStyle}">
    <Grid HorizontalAlignment="Stretch" >
        <StackPanel>
        <GroupBox Header="Information"  Margin="0,0,0,10" >
            <Label Margin="10,10,10,110">some useful information, dynamically updated</Label>
        </GroupBox>
        <GroupBox Header="Available actions" Margin="0,10,0,10">
            <StackPanel>
                <Label Margin="10,10,10,10">action 1</Label>
                <Label Margin="10,10,10,10">action 2</Label>
                <Label Margin="10,10,10,10">action 3</Label>
                    <!--<Image Width="0" Height="0" Source="{Binding HeaderImage}"/>-->
                </StackPanel>
        </GroupBox>
        </StackPanel>
    </Grid>
</ItemsControl>

      

And my model code (set as the data context for my custom control)

internal class StartPageViewPresentationModel : IStartPageViewPresentationModel
{
    public StartPageViewPresentationModel(IStartPageView view)
    {
        HeaderImage = new BitmapImage(new Uri(@"Images/home_16.png", UriKind.Relative)) { CacheOption = BitmapCacheOption.Default };

        HeaderInfo = "Start Page";

        View = view;
        View.Model = this;
    }

    public BitmapImage HeaderImage { get; set; }

    public string HeaderInfo { get; set; }

    public IStartPageView View { get; set; }
}

      

If I uncomment the tag in the custom control then the image will appear in both the control and the template area, if I comment it it won't appear in either of them. Linking text to a template works fine

I'm at a loss.

thank

Trevor

+1


source to share


1 answer


A few suggestions:

  • Have you tried an absolute URI for an image?
  • HeaderImage

    can be of type ImageSource

    rather than more restrictive BitmapImage

    .


I suspect what is happening UserControl

because the path is relative and correct based on location UserControl

. Therefore, the image is cached and works from the template.

However, when you comment on this, the image will be removed from the location Style

, what could be wrong?

+2


source







All Articles