2015-06-13 3 views
5

में लोड आरटीएफ मैं एमवीवीएम के लिए नया हूं और मैं mvvm का उपयोग कर रिचटेक्स्टबॉक्स में एक आरटीएफ फ़ाइल लोड करना चाहता हूं, लेकिन पाठ मेरे richtextbox में प्रदर्शित नहीं होता है। ऐसा लगता है कि RichTextBox ViewModel में कमांड को रखने का प्रयास करते समय निपटने के लिए बहुत जटिल है। मुझे यकीन नहीं है कि मैं गलत कहां जाता हूं।बाइंडबल रिचटेक्सबॉक्स एमवीवीएम wpf

ViewModel

FlowDocument _script; 
public FlowDocument Script 
    { 
     get { return _script; } 
     set { _script = value; RaisePropertyChanged("Script"); } 
    } 
. 
. 
. 
private void LoadScript() 
    { 
     openFile.InitialDirectory = "C:\\"; 

     if (openFile.ShowDialog() == true) 
     { 
      string originalfilename = System.IO.Path.GetFullPath(openFile.FileName); 

      if (openFile.CheckFileExists) 
      { 
       Script = new FlowDocument(); 
       TextRange range = new TextRange(Script.ContentStart, Script.ContentEnd); 
       FileStream fStream = new FileStream(originalfilename, System.IO.FileMode.OpenOrCreate); 
       range.Load(fStream, DataFormats.Rtf); 
       fStream.Close(); 
      } 
     } 
    } 

देखें

DataContext="{Binding ScriptBreakdownViewModel, Source={StaticResource Locator}}"> 

<Grid> 
    <RichTextBox 
     Local:RichTextBoxHelper.DocumentRtf="{Binding Script}" 
     x:Name="rtfMain" 
     HorizontalAlignment="Left" 
     Width="673" 
     VerticalScrollBarVisibility="Visible" 
     Margin="0,59,0,10.4" 
     /> 

RichTextBoxHelper

public class RichTextBoxHelper : DependencyObject 
{ 
    public static string GetDocumentRtf(DependencyObject obj) 
    { 
     return (string)obj.GetValue(DocumentRtfProperty); 
    } 
    public static void SetDocumentRtf(DependencyObject obj, string value) 
    { 
     obj.SetValue(DocumentRtfProperty, value); 
    } 
    public static readonly DependencyProperty DocumentRtfProperty = 
     DependencyProperty.RegisterAttached(
     "DocumentRtf", 
     typeof(string), 
     typeof(RichTextBoxHelper), 
     new FrameworkPropertyMetadata 
     { 
      BindsTwoWayByDefault = true, 
      PropertyChangedCallback = (obj, e) => 
      { 
       var richTextBox = (RichTextBox)obj; 

       // Parse the XAML to a document (or use XamlReader.Parse()) 
       var Rtf = GetDocumentRtf(richTextBox); 
       var doc = new FlowDocument(); 
       var range = new TextRange(doc.ContentStart, doc.ContentEnd); 

       range.Load(new MemoryStream(Encoding.UTF8.GetBytes(Rtf)), 
        DataFormats.Rtf); 

       // Set the document 
       richTextBox.Document = doc; 

       // When the document changes update the source 
       range.Changed += (obj2, e2) => 
       { 
        if (richTextBox.Document == doc) 
        { 
         MemoryStream buffer = new MemoryStream(); 
         range.Save(buffer, DataFormats.Rtf); 
         SetDocumentRtf(richTextBox, 
          Encoding.UTF8.GetString(buffer.ToArray())); 
        } 
       }; 
      } 
     }); 
} 

और मॉडल

FlowDocument _script; 
    public FlowDocument Script // the Name property 
    { 
     get { return _script; } 
     set { _script = value; NotifyPropertyChanged("Script"); } 
    } 
+0

क्या इससे आपको किसी भी तरह से मदद मिलती है? http://www.codeproject.com/Articles/137209/ बाइंडिंग- और- स्टाइलिंग-text-to-a-RichTextBox-in-WPF – niksofteng

+0

क्या व्यूमोडेल में आपकी 'लोडस्क्रिप्ट' विधि निश्चित रूप से कॉल की जा रही है? –

उत्तर

1

मैंने उपरोक्त नमूना कोड में अंतर को भरने की कोशिश की, लेकिन आपकी निर्भरता संपत्ति कभी ट्रिगर नहीं हुई है।

इसके बजाय, मैं XAML थोड़ा बदलकर पॉप्युलेट करने के लिए RichTextBox मिला:

<Button Height="30" Width="70" VerticalAlignment="Top" Command="{Binding OpenFileCommand}" CommandParameter="{Binding ElementName=myRichTextBox}">Load</Button> 
    <RichTextBox Name="myRichTextBox" 
     HorizontalAlignment="Left" 
     Width="673" 
     VerticalScrollBarVisibility="Visible" 
     Margin="0,59,0,10.4"></RichTextBox> 

एक बटन OpenFileCommand करने के लिए बाध्य है, जो ViewModel पर एक संपत्ति है नहीं है। यह RichTextBox को कमांड के पैरामीटर के रूप में पास करता है। मैंने ViewModel से कमांड पर LoadScript() विधि को स्थानांतरित कर दिया है।

public class OpenFileCommand : ICommand 
{ 
    private OpenFileDialog openFile = new OpenFileDialog(); 

    public bool CanExecute(object parameter) 
    { 
     return true; 
    } 

    public void Execute(object parameter) 
    { 
     var rtf = parameter as RichTextBox; 
     rtf.Document = LoadScript(); 
    } 

    public event EventHandler CanExecuteChanged; 

    private FlowDocument LoadScript() 
    { 
     openFile.InitialDirectory = "C:\\"; 

     if (openFile.ShowDialog() == true) 
     { 
      string originalfilename = System.IO.Path.GetFullPath(openFile.FileName); 

      if (openFile.CheckFileExists) 
      { 
       var script = new FlowDocument(); 
       TextRange range = new TextRange(script.ContentStart, script.ContentEnd); 
       FileStream fStream = new FileStream(originalfilename, System.IO.FileMode.OpenOrCreate); 
       range.Load(fStream, DataFormats.Rtf); 
       fStream.Close(); 
       return script; 
      } 
     } 

     return null; 
    } 
} 
1

मैंने सप्ताहांत में इसे थोड़ा और अधिक खेला। आपके RichTextBoxHelper को नहीं कहा जा रहा है, क्योंकि प्रकार गलत हैं। यदि इसे इस तरह दिखने के लिए संशोधित किया गया है:

public class RichTextBoxHelper : DependencyObject 
{ 
    public static FlowDocument GetDocumentRtf(DependencyObject obj) 
    { 
     return (FlowDocument)obj.GetValue(DocumentRtfProperty); 
    } 
    public static void SetDocumentRtf(DependencyObject obj, FlowDocument value) 
    { 
     obj.SetValue(DocumentRtfProperty, value); 
    } 
    public static readonly DependencyProperty DocumentRtfProperty = 
     DependencyProperty.RegisterAttached(
     "DocumentRtf", 
     typeof(FlowDocument), 
     typeof(RichTextBoxHelper), 
     new FrameworkPropertyMetadata 
     { 
      BindsTwoWayByDefault = true, 
      PropertyChangedCallback = (obj, e) => 
      { 
       var richTextBox = (RichTextBox)obj; 
       richTextBox.Document = e.NewValue as FlowDocument; 
      } 
     }); 
} 

फिर यह हिट हो जाता है, और RichTextBox दस्तावेज़ प्रॉपर्टी को ठीक से सेट करता है।

संबंधित मुद्दे