пятница, 5 августа 2011 г.
среда, 12 января 2011 г.
WPF - create DataTemplate from FrameworkElement
Yet another code snippet for future reference.
I'm not quite sure if it works in all cases, but it does work for Canvas.
DataTemplate GetElementAsTemplate(FrameworkElement element) {
StringBuilder builder = new StringBuilder();
builder.AppendFormat(
"{0} ",
XamlWriter.Save(element));
using(MemoryStream stream = new MemoryStream()) {
using(StreamWriter writer = new StreamWriter(stream)) {
writer.Write(builder.ToString());
writer.Flush();
stream.Position = 0;
DataTemplate template = (DataTemplate)XamlReader.Load(stream);
return template;
}
}
}
I'm not quite sure if it works in all cases, but it does work for Canvas.
DataTemplate GetElementAsTemplate(FrameworkElement element) {
StringBuilder builder = new StringBuilder();
builder.AppendFormat(
"
XamlWriter.Save(element));
using(MemoryStream stream = new MemoryStream()) {
using(StreamWriter writer = new StreamWriter(stream)) {
writer.Write(builder.ToString());
writer.Flush();
stream.Position = 0;
DataTemplate template = (DataTemplate)XamlReader.Load(stream);
return template;
}
}
}
Подписаться на:
Сообщения (Atom)
Random thoughts, ideas and questions on .NET development