0
我正在WPF中執行一個項目,在這個項目中用戶製作卡片,並且程序在移動這個卡片的屏幕上顯示這些卡片。將BitmapImage綁定到圖像控件
這張卡片有一個名稱,一個說明,可以有一個圖像或沒有。這個模板是下一個:
<Style x:Key="YellowCard" BasedOn="{StaticResource BaseItemStyle}" TargetType="{x:Type local:tarjeta}">
<Setter Property="Width" Value="100" />
<Setter Property="Height" Value="100" />
<Setter Property="Background" Value="{x:Null}" />
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Border BorderThickness="1" BorderBrush="White" Margin="1">
<StackPanel Background="Yellow">
<Image Name="image" Stretch="Fill" ... />
<Label
Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:tarjeta}}, Path=Nombre}"
HorizontalAlignment="Center"
VerticalAlignment="Top"
Foreground="Black"
FontSize="10"/>
<Label
Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:tarjeta}}, Path=Descripcion}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Foreground="Black"
FontSize="8"/>
</StackPanel>
</Border>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
卡類是下一個:
class tarjeta : ContentControl, ICloneable
{
private string nombre;
private string descripcion;
public ArrayList historial;
private string estilo;
byte[] arrayImagen;
string nombreImagen;
string strImagen;
BitmapImage imagen;
ImageSource imagenSource;
public tarjeta()
{
nombre = "";
descripcion = "";
historial = new ArrayList();
}
public tarjeta(string name, string desc)
{
nombre = name;
descripcion = desc;
historial = new ArrayList();
estilo = "YellowCard";
}
public tarjeta(string name, string desc, ArrayList hist)
{
nombre = name;
descripcion = desc;
historial = hist;
estilo = "YellowCard";
}
public tarjeta(string name, string desc, ArrayList hist, string style)
{
nombre = name;
descripcion = desc;
historial = hist;
estilo = style;
}
public tarjeta(string name, string desc, string nameImagen, string strImage, byte[] array)
{
nombre = name;
descripcion = desc;
historial = new ArrayList();
nombreImagen = nameImagen;
strImagen = strImage;
arrayImagen = array;
imagen = new BitmapImage();
}
public string Nombre
{
get { return this.nombre; }
set { this.nombre = value; }
}
public string Descripcion
{
get { return this.descripcion; }
set { this.descripcion = value; }
}
public string Estilo
{
get { return this.estilo; }
set { this.estilo = value; }
}
public byte[] ArrayImagen
{
get { return this.arrayImagen; }
set { this.arrayImagen = value; }
}
public string NombreImagen
{
get { return this.nombreImagen; }
set { this.nombreImagen = value; }
}
public string StrImagen
{
get { return this.strImagen; }
set { this.strImagen = value; }
}
public BitmapImage Imagen
{
get { return this.imagen; }
set { this.imagen = value; }
}
public ImageSource ImagenSource
{
get { return this.Imagen as ImageSource; }
}
public object Clone()
{
return this.MemberwiseClone();
}
public BitmapImage crearImagen(byte[] imageData)
{
// crear un bitmap para la imagen
BitmapImage bitImg = new BitmapImage();
try
{
// asignar los bytes obtenidos de la BBDD al bitmap
// se cogera el primer registro el campo 2 que contiene los bytes de la imagen
bitImg.BeginInit();
System.IO.Stream ms = new System.IO.MemoryStream(imageData);
bitImg.StreamSource = ms;
bitImg.EndInit();
bitImg.Freeze();
}
catch (Exception ex)
{
MessageBox.Show("Error: No se puede obtener la imagen " + ex.Message);
}
return bitImg;
}
}
卡的信息(名稱,描述和圖片),我從一個.xml文件獲得它。
問題是: 我如何使用樣式的圖像控件做裝訂卡的圖像? 謝謝!
您好!感謝您的回答,但它不起作用。 crearImagen()工作但綁定不起作用,程序不顯示圖像。 – Miguel
@Miguel:是否在構造函數中調用了「crearImagen」?如果稍後調用crearImagen,則需要實現依賴屬性或INotifyPropertyChanged接口,並在'Imagen'屬性中引發'property changed'事件。 –
現在它的工作!問題是當我從.xml文件讀取時,程序使用了其他沒有byte []的構造函數,沒有strImagen和沒有Imagen。感謝您的幫助!! – Miguel