私はバイナリ型のプロパティを持つデータベースを持っています。私はバイト配列をデータベースに挿入し、それらを取り出して画像を表示できるようにしたいと思います。私のコードを設定した方法では、私のバイト[]をgetterメソッドのイメージに変換してイメージを表示できるようにしたいと思っています。Imageオブジェクトを返すbyte []オブジェクトのゲッター
私は実行であることを持っているコード:
[PropertyDefinition("0B79C12F-21F4-4ECA-BF4F-E694B9DE73DB")]
[Display(Name = "Computer_AgentLastContactedIcon", Description = "Computer_AgentLastContactedIcon_Desc", Order = 5, ResourceType = typeof(EntityMetadataStrings))]
public byte[] AgentLastContactedIcon { get; set; }
問題はしかし、これは「System.Byte []」私のデータグリッドではなく、画像を表示することであるに。私は次のように私のgetterメソッドとsetterメソッドを変更した画像を表示するattemtで
:
[PropertyDefinition("0B79C12F-21F4-4ECA-BF4F-E694B9DE73DB")]
[Display(Name = "Computer_AgentLastContactedIcon", Description = "Computer_AgentLastContactedIcon_Desc", Order = 5, ResourceType = typeof(EntityMetadataStrings))]
public byte[] AgentLastContactedIcon;
public Image _AgentLastContactedIcon
{
get
{
MemoryStream ms = new MemoryStream(AgentLastContactedIcon);
Image img = Image.FromStream(ms);
return img;
}
set
{
ImageConverter converter = new ImageConverter();
byte[] array = (byte[])converter.ConvertTo(value, typeof(byte[]));
AgentLastContactedIcon = array;
}
}
でも、私はこのエラーを取得しています: 「属性 『PropertyDefinitionに』は、この宣言の型に有効ではありません。これは 'property、indexer'宣言でのみ有効です。 "
スタックオーバーフローに関する別の記事で私が見つけたアドバイスに従って、私は "public byte [] agentLastContactedIcon;"を移動しました。上:
public byte[] AgentLastContactedIcon;
[PropertyDefinition("0B79C12F-21F4-4ECA-BF4F-E694B9DE73DB")]
[Display(Name = "Computer_AgentLastContactedIcon", Description = "Computer_AgentLastContactedIcon_Desc", Order = 5, ResourceType = typeof(EntityMetadataStrings))]
public Image _AgentLastContactedIcon
{
get
{
MemoryStream ms = new MemoryStream(AgentLastContactedIcon);
Image img = Image.FromStream(ms);
return img;
}
set
{
ImageConverter converter = new ImageConverter();
byte[] array = (byte[])converter.ConvertTo(value, typeof(byte[]));
AgentLastContactedIcon = array;
}
}
しかしそうすることは私にエラーを与えた:「タイプの関連メタデータタイプ 『X』は、次の未知のプロパティまたはフィールドを含んでいます。_AgentLastContactedIconをこれらのメンバーの名前はの名前と一致していることを確認してください私は、データベース内の項目を表示するには、次のSilverlightを使用している主なタイプのプロパティ
:任意の助けを事前に
<ctrls:CustomDataGrid
x:Name="ComputersDataGrid"
Grid.Row="1"
Style="{StaticResource DataGridStyle}"
ItemsSource="{Binding ItemsSource}"
SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
SelectionMode="{Binding SelectionMode}"
PropertyDefinitions="{Binding PropertyDefinitions}" />
おかげ
を!コンバーターを実装していただきありがとうございます。私はそれを動作させるのに少し問題があります。私はここで文字列を別の文字列に変換して動作するようにしようとしています。ここ
xmlns:converter="clr-namespace:AccessData.IAS.Shared.Web.Resources"
:
<UserControl.Resources>
<converter:ImageConverter x:Key="ImageConverter" />
、ここで:私はここに私の.xamlファイルでそれを使用しよう
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Globalization;
using System.Windows.Data;
namespace IAS.Shared.Web.Resources
{
[ValueConversion(typeof(string), typeof(string))]
public class ImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
String str = (string)value;
str = "changed";
return str;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
string str = (string)value;
str = "changedBack";
return str;
}
}
}
:ここに私のサンプル変換器である
<ctrls:CustomDataGrid x:Name="ComputersDataGrid"
Grid.Row="1"
Style="{StaticResource DataGridStyle}"
ItemsSource="{Binding ItemsSource, Converter={StaticResource ImageConverter}}"
SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
SelectionMode="{Binding SelectionMode}"
PropertyDefinitions="{Binding PropertyDefinitions}">
私の文字列は変更されません。私はまた、buidエラーを取得しています "タイプ 'コンバータ:ImageConverter'が見つかりませんでしたアセンブリ参照がないことを確認し、参照されているすべてのアセンブリがビルドされていることを確認します"とタグ 'ImageConverter'はXML名前空間'clr-namespace:IAS.Shared.Wen.Resources'」を参照してください。しかし、私がもう一度ビルドすれば、それは走ります。私のコードで、文字列の変換を妨げる可能性のある問題がありますか?再度、感謝します!
FCL自体にも、「IValueConverter」と同様のインターフェイスが実装されていますか? – abatishchev