2010-12-03 18 views
1

任意のVisualをユーザーに表示する、シンプルで洗練された方法を探しています。私が頭を離して考えることができる唯一の方法は、ブラシでそれを叩き、それをScrollViewerのRectangleにペイントすることです。正確には最良の選択肢ではありません。WPFアプリケーションでビジュアルを表示するのに便利な方法は?

+0

このような低レベルのネクタイの必要性は何ですか? –

+1

これは、あなたが本当に必要とするもののより高いレベルの説明から恩恵を受けるそれらの質問の1つであり、あなたに代替案を与えることができるようです。 – Tergiver

+0

@Aaron @Tergiver選択肢はありません。私はビジュアルを組み合わせてXpsDocumentsを作成します。私はそれらをビジュアルとして知っていますが、ビジュアルから拡張するものであってもかまいません。 – Will

答えて

0

私の(現在の)答えに(部分)の例を見てみたい場合は

は、 Visual.AddVisualに例を見てみましょうかXpsDocumentを開き、それをDocumentViewerに表示します。私はそれほど複雑ではありませんが、私はすでにこのようにするためのインフラを持っています。その100%ではありませんが、それは動作します。

まず、行動私はDocumentViewer.Documentに特異的に結合することができるように(そのfriggen POCO、urgh):

public sealed class XpsDocumentBinding 
{ 
    #region Document 
    /// <summary> 
    /// The <see cref="DependencyProperty"/> for <see cref="Document"/>. 
    /// </summary> 
    public static readonly DependencyProperty DocumentProperty = 
     DependencyProperty.RegisterAttached(
      "Document", 
      typeof(XpsDocument), // 
      typeof(XpsDocumentBinding), 
      new UIPropertyMetadata(null, OnDocumentChanged)); 

    /// <summary> 
    /// Gets the value of the <see cref="DocumentProperty">Document attached property</see> on the given <paramref name="target"/>. 
    /// </summary> 
    /// <param name="target">The <see cref="DependencyObject">target</see> on which the property is set.</param> 
    public static XpsDocument GetDocument(DependencyObject target) 
    { 
     return (XpsDocument)target.GetValue(DocumentProperty); 
    } 

    /// <summary> 
    /// Sets the <paramref name="value"/> of the <see cref="DocumentProperty">Document attached property</see> on the given <paramref name="target"/>. 
    /// </summary> 
    /// <param name="dependencyObject">The <see cref="DependencyObject">target</see> on which the property is to be set.</param> 
    /// <param name="value">The value to set.</param> 
    public static void SetDocument(DependencyObject target, XpsDocument value) 
    { 
     target.SetValue(DocumentProperty, value); 
    } 

    /// <summary> 
    /// Called when Document changes. 
    /// </summary> 
    /// <param name="sender">The sender.</param> 
    /// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param> 
    private static void OnDocumentChanged(object sender, DependencyPropertyChangedEventArgs e) 
    { 
     var viewer = sender as DocumentViewer; 
     if (viewer == null) 
      throw new InvalidOperationException(
       "This behavior is only valid on DocumetViewers."); 
     var doc = e.NewValue as XpsDocument; 
     if (doc == null) 
      return; 
     viewer.Document = doc.GetFixedDocumentSequence(); 
    } 
    #endregion 
} 

その後、私のモデルでは、私はXpsDocument

var pack = PackageStore.GetPackage(_uri); 
if (pack != null) 
    return new XpsDocument(pack, CompressionOption.SuperFast, _uri.AbsoluteUri); 

MemoryStream ms = new MemoryStream(2048); 
Package p = Package.Open(ms, FileMode.Create, FileAccess.ReadWrite); 
PackageStore.AddPackage(_uri, p); 
XpsDocument doc = new XpsDocument(p, CompressionOption.SuperFast, _uri.AbsoluteUri); 

var writer = XpsDocument.CreateXpsDocumentWriter(doc); 
var collator = writer.CreateVisualsCollator(); 
// write the visuals using our collator 
collator.BeginBatchWrite(); 
collator.Write(Visual); 
collator.EndBatchWrite(); 
p.Flush(); 
return doc; 
としての私のビジュアルを公開

DocumentViewerを追加して、変換メソッドの結果をビヘイビア経由でバインドしてください。ここにどこかのショートカットがあると確信しています...

1

Visualには位置もサイズもないので、どのように行うことができるのかわかりません。おそらくFrameworkElementに固執し、そのスタイルを作成しますか?

+0

さて、サイズと位置はそれが何であるかによって決めることができます。これは私が探しているものです。私はそれを入れることができます。 – Will

2

VisualをホストするFrameworkElementか、Visualから派生するオブジェクトをホストする汎用ラッパーを継承するラッパーを作成できます。あなたは、複数の視覚的なホストでそれを平手打ちすることですUsing DrawingVisual Objects

+0

私はビジュアルを持って別のビジュアルに入れても、私はそれほど遠くにはありません。私はDrawingVisualで遊んでいた可能性があり、イメージに入れていたかもしれませんが、ウサギの穴を塞ぐようなものでした...どのようにして、ユーザーが簡単にズームしてパンすることができるビジュアル? – Will

+0

リンクはVisual.AddVisualを指していますが、この例を見ると、 'FrameworkElement'に' Visual'をホストする必要があります - それを 'DrawingVisual'の中に入れる必要はありません。混乱をおかけして申し訳ありません。 – Samuel

関連する問題