none
WPF中的RichTextBox控件怎么添加背景图片? RRS feed

  • 问题

  • 如题。

    希望的效果是点击一个按钮让客户选择一张图片,完毕之后这张图片自动成为RichTextBox控件编辑区的背景图片,上面还是一样能够写字,有图片等等元素~

    谢谢

    2010年5月26日 8:05

答案

  • 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 Members

            public 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) 方法。

    2010年5月28日 7:57
  • 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.
    2010年5月28日 10:32
    版主

全部回复

  • 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 Members

            public 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) 方法。

    2010年5月28日 7:57
  • 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.
    2010年5月28日 10:32
    版主