图像源属性 - 内部 DataTemplate 和 UserControl.Resources - 来自不同程序集的图像

发布于 2024-12-08 03:40:56 字数 2591 浏览 0 评论 0原文

如果我不在数据模板内,则使用图像源属性就可以了。 否则他无法找到另一个名为“Images”的程序集中的图片。

XAML,有效。我可以看到由“图像”程序集持有的图像:

<UserControl x:Class="Views.ViewUserInfo"
             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" 
             mc:Ignorable="d"              
             d:DesignHeight="300" d:DesignWidth="600">

    <StackPanel Orientation="Horizontal">
        <StackPanel Orientation="Horizontal" Margin="0,5,0,5">
            <TextBlock Text="Authorized: "/>
            <TextBlock Text="{Binding Path=IsAuthorized, Mode=OneWay}" VerticalAlignment="Center"/>
            <Image Width="16" Height="16" Source="/Images;Component/Img16/Ok.png" />
         </StackPanel>
    </StackPanel>
</UserControl>

不起作用:

<UserControl x:Class="Views.ViewUserInfo"
             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" 
             mc:Ignorable="d"              
             d:DesignHeight="300" d:DesignWidth="600">

    <UserControl.Resources>
        <DataTemplate DataType="{x:Type System:Boolean}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="DataTemplate has been found " />
                <Image Width="16" Height="16" Source="/Images;Component/Img16/Ok.png" />
            </StackPanel>
            <DataTemplate.Resources>
                <!--simplyfied, Triggers removed...--->
            </DataTemplate.Resources>
        </DataTemplate>
    </UserControl.Resources>

    <StackPanel Orientation="Horizontal">
        <StackPanel Orientation="Horizontal" Margin="0,5,0,5">
            <TextBlock Text="Authorized: "/>
            <ContentPresenter Content="{Binding Path=IsAuthorized, Mode=OneWay}" VerticalAlignment="Center"/>
            <!--IsAuthorized.GetType() = typeof(System.Boolean)-->
         </StackPanel>
    </StackPanel>
</UserControl>

他实际上在 DataTemplate 中,因为他向我显示了文本“DataTemplate 已找到”,但我看不到任何图片。 这里有什么问题吗?

Using the Image Source Property just works, if I'm not inside a DataTemplate.
Otherwise he cannot find the picture which is in another assembly named "Images".

XAML, that works. I can see the Image, holded by the "Images" assembly:

<UserControl x:Class="Views.ViewUserInfo"
             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" 
             mc:Ignorable="d"              
             d:DesignHeight="300" d:DesignWidth="600">

    <StackPanel Orientation="Horizontal">
        <StackPanel Orientation="Horizontal" Margin="0,5,0,5">
            <TextBlock Text="Authorized: "/>
            <TextBlock Text="{Binding Path=IsAuthorized, Mode=OneWay}" VerticalAlignment="Center"/>
            <Image Width="16" Height="16" Source="/Images;Component/Img16/Ok.png" />
         </StackPanel>
    </StackPanel>
</UserControl>

Does not work:

<UserControl x:Class="Views.ViewUserInfo"
             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" 
             mc:Ignorable="d"              
             d:DesignHeight="300" d:DesignWidth="600">

    <UserControl.Resources>
        <DataTemplate DataType="{x:Type System:Boolean}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="DataTemplate has been found " />
                <Image Width="16" Height="16" Source="/Images;Component/Img16/Ok.png" />
            </StackPanel>
            <DataTemplate.Resources>
                <!--simplyfied, Triggers removed...--->
            </DataTemplate.Resources>
        </DataTemplate>
    </UserControl.Resources>

    <StackPanel Orientation="Horizontal">
        <StackPanel Orientation="Horizontal" Margin="0,5,0,5">
            <TextBlock Text="Authorized: "/>
            <ContentPresenter Content="{Binding Path=IsAuthorized, Mode=OneWay}" VerticalAlignment="Center"/>
            <!--IsAuthorized.GetType() = typeof(System.Boolean)-->
         </StackPanel>
    </StackPanel>
</UserControl>

He's actually in the DataTemplate, because he shows me the Text "DataTemplate has been found" but i cannot see any picture..
Whats the problem here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淑女气质 2024-12-15 03:40:56

你说你看到了文本框,那怎么可能,你的模板是空的,你的StackPanel只是你的DataTemplate中的一个资源。尝试删除 行,或者添加您的

You said you see the text box, how can that be, your template is empty, your StackPanel is just a resource in your DataTemplate. Try removing the <DataTemplate.Resources> and </DataTemplate.Resources> lines or maybe add your <!--simplyfied, Triggers removed...--->.

眼泪淡了忧伤 2024-12-15 03:40:56

不要使用 ContentPresenter。使用内容控件。

   <ContentControl Content="{Binding Path=IsAuthorized, Mode=OneWay}" 
                     VerticalAlignment="Center"/> 

Dont use ContentPresenter. Use ContentControl.

   <ContentControl Content="{Binding Path=IsAuthorized, Mode=OneWay}" 
                     VerticalAlignment="Center"/> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文