For now, RadRichTextBox does not support dragging or pasting files. So I override the dragevent.
public
class
UserFile
{
public
string
name;
public
string
absoluteAddress;
public
string
image;
}
public
class
DragableRichTextBox : RadRichTextBox
{
public
static
readonly
DependencyProperty UserFilesProperty;
static
int
dirCount;
static
DragableRichTextBox()
{
FrameworkPropertyMetadata metadata =
new
FrameworkPropertyMetadata(
new
List<UserFile>(), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault);
UserFilesProperty = DependencyProperty.Register(
"UserFiles"
,
typeof
(List<UserFile>),
typeof
(RadRichTextBox), metadata);
dirCount = 0;
}
public
List<UserFile> UserFiles
{
get
{
userFiles.Clear();
XamlFormatProvider provider =
new
XamlFormatProvider();
XmlDocument xmlDocument =
new
XmlDocument();
string
data = provider.Export(
this
.Document);
xmlDocument.LoadXml(data);
scanFileAndAddIamge(xmlDocument.FirstChild);
SetValue(UserFilesProperty, userFiles);
return
(List<UserFile>)GetValue(UserFilesProperty);
}
set
{ SetValue(UserFilesProperty, value); }
}
List<UserFile> userFiles;
public
string
SaveDirectory;
public
DragableRichTextBox()
{
this
.AllowDrop =
true
;
this
.AddHandler(RichTextBox.DropEvent,
new
DragEventHandler(dropIn),
true
);
this
.AddHandler(RichTextBox.DragOverEvent,
new
DragEventHandler(dropOver),
true
);
userFiles =
new
List<UserFile>();
string
basePath = @
"C:\123\temp"
;
checkPath(basePath);
for
(
int
i = dirCount; i < 1000; ++i)
{
if
(checkPath(basePath + @
"\"
+ i))
{
SaveDirectory = basePath + @
"\"
+ i;
dirCount = i;
break
;
}
}
}
bool
checkPath(
string
path)
{
if
(!Directory.Exists(path))
{
Directory.CreateDirectory(path);
return
true
;
}
else
if
(dirCount == 0)
{
Directory.Delete(path,
true
);
}
return
false
;
}
private
void
dropIn(
object
obj, DragEventArgs de)
{
if
(de.Data.GetDataPresent(DataFormats.FileDrop))
{
string
[] files = (
string
[])de.Data.GetData(DataFormats.FileDrop);
foreach
(
string
file
in
files)
{
UserFile f =
new
UserFile();
System.Windows.Controls.Image image =
new
System.Windows.Controls.Image();
image.Source = GetIcon(file);
image.Height = image.Source.Height;
image.Width = image.Source.Width;
PngBitmapEncoder pictureEncoder =
new
PngBitmapEncoder();
pictureEncoder.Frames.Add(BitmapFrame.Create((BitmapSource)image.Source));
FileStream fs =
new
FileStream(String.Format(SaveDirectory +
"/{0}.png"
, userFiles.Count), FileMode.OpenOrCreate, FileAccess.Write);
pictureEncoder.Save(fs);
fs.Close();
string
path = String.Format(SaveDirectory +
"/{0}.png"
, userFiles.Count);
f.image = path;
Uri uri =
new
Uri(path);
BitmapImage image1 =
new
BitmapImage(uri);
image.Source = image1;
StackPanel stk =
new
StackPanel();
stk.Children.Add(image);
Telerik.Windows.Controls.Label l =
new
Telerik.Windows.Controls.Label();
for
(
int
i = file.Length - 1; i >= 0; --i)
{
if
(file[i] ==
'\\'
)
{
l.Content = file.Remove(0, i + 1);
break
;
}
}
Telerik.Windows.Controls.Label l1 =
new
Telerik.Windows.Controls.Label();
l1.Content = file;
l1.Visibility = Visibility.Collapsed;
stk.Children.Add(l);
stk.Children.Add(l1);
f.name = l.Content.ToString();
f.absoluteAddress = l1.Content.ToString();
userFiles.Add(f);
Section section =
new
Section();
Paragraph paragraph =
new
Paragraph();
InlineUIContainer container =
new
InlineUIContainer();
container.UiElement = stk;
container.Height = 25 + image.Height;
double
x = l.Content.ToString().Length;
container.Width = x * 6.5 > image.Width ? x * 6.5 : image.Width;
paragraph.Inlines.Add(container);
section.Blocks.Add(paragraph);
this
.Document.Sections.Add(section);
RecreateUI();
container.Width = stk.ActualWidth;
}
}
RecreateUI();
}
private
void
dropOver(
object
obj, DragEventArgs de)
{
if
(de.Data.GetDataPresent(DataFormats.FileDrop))
{
de.Effects = DragDropEffects.Copy;
}
else
if
(de.Data.GetDataPresent(DataFormats.Text) | de.Data.GetDataPresent(DataFormats.Bitmap) | de.Data.GetDataPresent(DataFormats.CommaSeparatedValue) | de.Data.GetDataPresent(DataFormats.Dib) | de.Data.GetDataPresent(DataFormats.Dif) | de.Data.GetDataPresent(DataFormats.EnhancedMetafile) | de.Data.GetDataPresent(DataFormats.FileDrop))
{
de.Effects = DragDropEffects.Scroll;
}
else
{
de.Effects = DragDropEffects.None;
}
}
private
ImageSource GetIcon(
string
fileName)
{
Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(fileName);
return
System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(
icon.Handle,
new
Int32Rect(0, 0, icon.Width, icon.Height),
BitmapSizeOptions.FromEmptyOptions());
}
private
void
RecreateUI()
{
if
(
this
.ActiveEditorPresenter !=
null
)
{
this
.ActiveEditorPresenter.RecreateUI();
this
.UpdateEditorLayout();
}
}
private
void
scanFileAndAddIamge(XmlNode node)
{
if
(node.ChildNodes.Count == 0)
return
;
foreach
(XmlNode xn
in
node.ChildNodes)
{
if
(xn.Name ==
"av:StackPanel"
)
{
UserFile f =
new
UserFile();
foreach
(XmlNode xn1
in
xn.ChildNodes)
{
if
(xn1.Name ==
"Label"
)
{
foreach
(XmlAttribute attribute
in
xn1.Attributes)
{
if
(attribute.Name ==
"Visibility"
&& attribute.Value ==
"Collapsed"
)
{
f.absoluteAddress = xn1.InnerText;
}
}
if
(xn1.Attributes.Count == 0)
f.name = xn1.InnerText;
}
if
(xn1.Name ==
"av:Image"
)
{
foreach
(XmlAttribute attribute
in
xn1.Attributes)
{
if
(attribute.Name ==
"Source"
)
{
for
(
int
i = attribute.Value.Length - 1; i >= 0; --i)
{
if
(attribute.Value[i] ==
'/'
)
{
attribute.Value = attribute.Value.Remove(0, i);
break
;
}
}
attribute.Value = SaveDirectory +
"/"
+ attribute.Value;
f.image = attribute.Value;
int
kk = 11;
kk++;
kk++;
}
}
}
}
userFiles.Add(f);
}
else
{
scanFileAndAddIamge(xn);
}
}
}
}
I can drag files now, but the files alway insert in the end of the RadDoucument. I don't know how to get the paragragh or section that my mosue point.
Please help me.