代码之家  ›  专栏  ›  技术社区  ›  kenwarner

关闭视图时不显示WPF图像

  •  0
  • kenwarner  · 技术社区  · 15 年前

    我有以下WPF申请

    <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是一个用户控件,定义为

    <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>
    

    当我在选项卡项之间切换时,有时图像显示,有时不显示。为什么会发生这种情况?

    1 回复  |  直到 15 年前
        1
  •  0
  •   kenwarner    15 年前

    据记录,我可以通过将ImageView更改为

    <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>
    

    但我仍然好奇为什么第一种方法不起作用