2009/04/02(Thu) 01:53:59 編集(投稿者)
うおおおおおおおおおおおおおお、noriさああああああああああああああああんんん!!!!
その通りでした、ありがとうございます!!!!!!!!!!_(__)_
BitmapImageをつかえば動いてくれました。
.NETとイメージ周りの使い方が全然違いますねT^T
>囚人さんの例では
>><GridViewColumn Header="image" CellTemplate="{StaticResource image}"/>
>Binding imageではなくStaticResource imageになっています
><Image Source="{Binding Path=ImageUri}"/>
XAMLの場合だけ直接Uriを記入できるんですよね。Imageクラスから直接Uriでオブジェクトを作れないのがちょっと不思議に思います。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WPFTest2
{
/// <summary>
/// Window1.xaml の相互作用ロジック
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
ListViewItem l = new ListViewItem();
BitmapImage bmp = new BitmapImage();
bmp.BeginInit();
bmp.UriSource = new Uri("c:\\1024.jpg");
bmp.EndInit();
ListViewItemProps.SetItemBitmapImage(l, bmp);
ListViewItemProps.SetItemName(l, "test");
this.listView1.Items.Add(l);
}
}
public static class ListViewItemProps
{
public static readonly DependencyProperty ItemBitmapImageProperty;
public static readonly DependencyProperty ItemNameProperty;
public static BitmapImage GetItemBitmapImage(DependencyObject obj)
{
return (BitmapImage)obj.GetValue(ItemBitmapImageProperty);
}
public static void SetItemBitmapImage(DependencyObject obj, BitmapImage value)
{
obj.SetValue(ItemBitmapImageProperty, value);
}
public static string GetItemName(DependencyObject obj)
{
return (string)obj.GetValue(ItemNameProperty);
}
public static void SetItemName(DependencyObject obj, string value)
{
obj.SetValue(ItemNameProperty, value);
}
static ListViewItemProps()
{
ItemNameProperty = DependencyProperty.Register("ItemName", typeof(string), typeof(ListViewItemProps));
ItemBitmapImageProperty = DependencyProperty.Register("ItemBitmapImage", typeof(BitmapImage), typeof(ListViewItemProps));
}
}
}
<Window x:Class="WPFTest2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPFTest2"
Title="Window1" Height="300" Width="300">
<Grid>
<ListView Margin="54,28,30,28" Name="listView1">
<ListView.Resources>
<DataTemplate x:Key="myItemTemplate1" DataType="ContentPresenter">
<StackPanel Orientation="Horizontal">
<Border Width="100" Height="50" BorderBrush="Gray" BorderThickness="2" CornerRadius="5" Margin="3">
<Image Source="{Binding RelativeSource={RelativeSource
AncestorType={x:Type ListViewItem}}, Path = (local:ListViewItemProps.ItemBitmapImage)}"></Image>
</Border>
<TextBlock Text = "{Binding RelativeSource={RelativeSource
AncestorType={x:Type ListViewItem}}, Path = (local:ListViewItemProps.ItemName)}"></TextBlock>
</StackPanel>
</DataTemplate>
</ListView.Resources>
<ListView.View>
<GridView>
<GridViewColumn Header="ヘッダ1"
CellTemplate="{StaticResource myItemTemplate1}"
/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>