【问题标题】:WPF CustomControl and Image bindingWPF CustomControl 和图像绑定
【发布时间】:2010-04-12 08:35:23
【问题描述】:

自从 2 天以来,我真的因为一个愚蠢的问题而生气。我已经在这里问过这个问题,但似乎我的问题迷失了,没有人会再看到它。所以这是我的简单问题:

我有一个项目包含一个CustomControl(一个库项目),这个自定义控件代码是继承自Window控件的。所以它有一个继承自它的 Icon 属性。在创建控件设计的 XAML 代码中,我想在 ResourceDictionary 的某个位置放置一个绑定到 Icon 属性的 Image。

...    
<Image Grid.Column="0" Margin="3" Width="27" Height="27" Source="{Binding Icon}" />
...

然后我有第二个项目(一个 WPF 应用程序项目)引用我的第一个项目并使用此自定义控件窗口,我在其中设置 Icon 属性。图标属性设置正确,因为我可以在任务栏中看到图标,但图像没有出现,好像我的绑定不起作用。

<SILU:FlatForm x:Class="SILU_MovieManager.WinMain"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:SILU="clr-namespace:SILU_Controls;assembly=SILU_Controls"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="SILU Movie Manager" Height="425" Width="682" Loaded="FlatForm_Loaded" Icon="/SILU_MovieManager;component/Resources/Images/Film.ico">
    <Grid>

    </Grid>
</SILU:FlatForm>

我真的不知道如何绑定它,这是我在这里找到的一种解决方案,但它对我不起作用。 (Solution)

【问题讨论】:

    标签: wpf image xaml binding custom-controls


    【解决方案1】:

    我还没有尝试过这个解决方案,这是通过代码和图标完成的

    <Window x:Class="WPFWindowAPP.IconLoader"
    
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    
    Title="WPFWindowAPP" Height="164" Width="405"
    
    >
    
    <Canvas>
    
    
        <Button Name="btn" Click="btnClick" Canvas.Top="40" Canvas.Right="90" Width="75">Load Icon</Button>
    
        <Image Name="icoDisplay" Canvas.Left="10" Canvas.Top="80" Stretch="None" />
    
    </Canvas>
    

        void btnClick(object sender, RoutedEventArgs e)        {
    

    IconImage ico = IconImage.ExtractAssociatedIcon(filePath.Text); 位图 bmp = ico.ToBitmap(); MemoryStream strm = new MemoryStream(); bmp.Save(strm, System.Drawing.Imaging.ImageFormat.Png);
    BitmapImage bmpImage = new BitmapImage();
    bmpImage.BeginInit();
    strm.Seek(0, SeekOrigin.Begin); bmpImage.StreamSource = strm; bmpImage.EndInit();
    icoDisplay.Source = bmpImage; }

    【讨论】:

    • 这是通过文件路径完成的,我的图标是来自 WPF 应用程序的资源,我无法应用此方法(我认为)。
    • 我找到了一种解决方案,但我不知道这是否适合您的情况,但无论如何尝试一下。将图标作为资源添加到项目中(不是嵌入式资源,有区别),然后使用 (Stream stream = Application.GetResourceStream(new Uri("\\comment.ico",UriKind.RelativeOrAbsolute)) 像这样访问它.Stream) { BitmapDecoder 解码器 = IconBitmapDecoder.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.None); BitmapSource 源 = 解码器.Frames[0]; this.test.Source = 来源; }
    • 谢谢,但我在自定义控件库中,我不能做这样的声明。MyImage.Source。我有一个从 Window 类和一个 ResourceDictionary 继承的类,我在其中定义了控件样式,例如
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-17
    • 2015-08-06
    • 2010-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多