积极答复者
WPF中的RichTextBox控件怎么添加背景图片?

问题
答案
-
1,创建一个ViewModel类用于绑定,要支持 INotifyPropertyChanged,以便改变背景图片.
public class YourAppViewModel : INotifyPropertyChanged
{
private string imageFilePath;
private List<byte> backgroundImage;
public List<byte> BackgroundImage
{
get
{
if (this.backgroundImage == null || this.backgroundImage.Count == 0)
this.backgroundImage = new List<byte>(File.ReadAllBytes(imageFilePath));
return this.backgroundImage;
}
set
{
if (this.backgroundImage != value)
{
this.backgroundImage = null;
this.OnPropertyChanged("BackgroundImage");
}
}
}public void ChangeBackgroundImage(string newImageFile)
{
if(this.imageFilePath != newImageFile)
{
this.imageFilePath = newImageFile;
this.BackgroundImage = null; //强制UI刷新,以获取新的图片数据。
}
}}
还需要一个ValueConverter,负责把byte[]转换成ImageBrush.ImageSource支持的BitmapImage类型
public class ByteImageConverter : IValueConverter
{
#region IValueConverter Memberspublic object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (targetType != typeof(ImageSource))
throw new InvalidOperationException("The target must be ImageSource or derived types");if (value != null && value is List<Byte>)
{
List<Byte> bytes = value as List<Byte>;
if (bytes.Count > 0)
{
MemoryStream stream = new MemoryStream(bytes.ToArray());
BitmapImage image = new BitmapImage();
image.BeginInit();
image.StreamSource = stream;
image.EndInit();
return image;
}
}
return null;
}public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}#endregion
}在xaml中设置绑定:
<converters:ByteImageConverter x:Key="imageConverter" />
设置背景图片刷:
<ImageBrush x:Key="richTextBoxBackground"
Stretch="Fill"
ImageSource="{Binding Path=BackgroundImage,Converter={StaticResource imageConverter}}" />绑定到RichTextBox背景上.
<RichTextBox Background="{StaticResource windowBackground}" ..../>
在你更改图片后,获取新图片的文件路径,然后调用:
YourAppViewModel.ChangeBackgroundImage(newImageFile) 方法。
- 已建议为答案 Aland LiModerator 2010年5月28日 10:32
- 已编辑 Skyseer 2010年5月28日 14:53 补充完整
- 已标记为答案 Aland LiModerator 2010年6月2日 10:46
-
Hi 梦心,
您可以参考Galactica的代码,不过有点复杂。简单点的话直接参考下面的xaml就行:
<RichTextBox> <RichTextBox.Background> <ImageBrush ImageSource="Images/Desert.jpg"/> </RichTextBox.Background> </RichTextBox>
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.- 已标记为答案 Aland LiModerator 2010年6月2日 10:46
全部回复
-
1,创建一个ViewModel类用于绑定,要支持 INotifyPropertyChanged,以便改变背景图片.
public class YourAppViewModel : INotifyPropertyChanged
{
private string imageFilePath;
private List<byte> backgroundImage;
public List<byte> BackgroundImage
{
get
{
if (this.backgroundImage == null || this.backgroundImage.Count == 0)
this.backgroundImage = new List<byte>(File.ReadAllBytes(imageFilePath));
return this.backgroundImage;
}
set
{
if (this.backgroundImage != value)
{
this.backgroundImage = null;
this.OnPropertyChanged("BackgroundImage");
}
}
}public void ChangeBackgroundImage(string newImageFile)
{
if(this.imageFilePath != newImageFile)
{
this.imageFilePath = newImageFile;
this.BackgroundImage = null; //强制UI刷新,以获取新的图片数据。
}
}}
还需要一个ValueConverter,负责把byte[]转换成ImageBrush.ImageSource支持的BitmapImage类型
public class ByteImageConverter : IValueConverter
{
#region IValueConverter Memberspublic object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (targetType != typeof(ImageSource))
throw new InvalidOperationException("The target must be ImageSource or derived types");if (value != null && value is List<Byte>)
{
List<Byte> bytes = value as List<Byte>;
if (bytes.Count > 0)
{
MemoryStream stream = new MemoryStream(bytes.ToArray());
BitmapImage image = new BitmapImage();
image.BeginInit();
image.StreamSource = stream;
image.EndInit();
return image;
}
}
return null;
}public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}#endregion
}在xaml中设置绑定:
<converters:ByteImageConverter x:Key="imageConverter" />
设置背景图片刷:
<ImageBrush x:Key="richTextBoxBackground"
Stretch="Fill"
ImageSource="{Binding Path=BackgroundImage,Converter={StaticResource imageConverter}}" />绑定到RichTextBox背景上.
<RichTextBox Background="{StaticResource windowBackground}" ..../>
在你更改图片后,获取新图片的文件路径,然后调用:
YourAppViewModel.ChangeBackgroundImage(newImageFile) 方法。
- 已建议为答案 Aland LiModerator 2010年5月28日 10:32
- 已编辑 Skyseer 2010年5月28日 14:53 补充完整
- 已标记为答案 Aland LiModerator 2010年6月2日 10:46
-
Hi 梦心,
您可以参考Galactica的代码,不过有点复杂。简单点的话直接参考下面的xaml就行:
<RichTextBox> <RichTextBox.Background> <ImageBrush ImageSource="Images/Desert.jpg"/> </RichTextBox.Background> </RichTextBox>
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.- 已标记为答案 Aland LiModerator 2010年6月2日 10:46