views:

68

answers:

1

I have the following WPF application

<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"        
    Title="Window1" Height="300" Width="300">
    <Grid>
        <TabControl>
            <TabItem Header="Test1">
                <local:ImageView />
            </TabItem>
            <TabItem Header="Test2">
                <local:ImageView />
            </TabItem>
            <TabItem Header="Test3">
                <local:ImageView />
            </TabItem>
        </TabControl>
    </Grid>
</Window>

ImageView is a UserControl defined as

<UserControl x:Class="ImageView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
    <Grid>
 <Image Source="pack://application:,,,/Resources/icon.png" Width="15" Height="15" />
    </Grid>
</UserControl>

When I switch between TabItems, sometimes the image displays, sometimes it doesn't. Why does this happen?

A: 

For the record, I was able to get around this problem by changing my ImageView to

<UserControl x:Class="ImageView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<Grid>
    <Grid.Resources>
        <BitmapImage x:Key="IconImageSource" UriSource="pack://application:,,,/Resources/icon.png" />
    </Grid.Resources>

    <Image Source="{StaticResource IconImageSource}" Width="15" Height="15" />
</Grid>

But I'm still curious why the first approach doesn't work

qntmfred