2011-11-29 107 views
1

转到编辑具有约束力不工作

由于在RichTextBox的XAML属性RichTextBox的形象是不是我创建了一个定制的RichTextbox,我可以用它的XAML属性交互依赖属性:

<local:RichTextUserControl RtfXaml="{Binding Path=Text, Converter={StaticResource RichTextBoxContentConverter}}" /> 

,我结合下述文字到XAML属性,并将其工作正常:

<Section xml:space=\"preserve\" HasTrailingParagraphBreakOnPaste=\"False\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"> 
    <Paragraph FontSize=\"20\" FontFamily=\"Segoe WP\" Foreground=\"#FFFFFFFF\" FontWeight=\"Normal\" FontStyle=\"Normal\" FontStretch=\"Normal\" TextAlignment=\"Left\"> 
     <Run Text=\"Some text without formatting\" /> 
     <Italic>Some italic text</Italic> 
     <Underline>I am UnderLined</Underline> 
    </Paragraph> 
</Section> 

我我通过转换器绑定它,在那里我搜索笑脸字符(例如:);):D等等......)并用图像替换它们,如果我在段落文本之间的某处插入下面的代码,崩溃:

<InlineUIContainer> 
    <Image Source="ApplicationIcon.png"/> 
</InlineUIContainer> 

(这是唯一的例外,当它是结合)

编辑

所以我发现这是一个坏的形式给出,我开始实施这样说:

<RichTextBox Tag="{Binding Path=MessageText}" TextWrapping="Wrap" Loaded="loaded"/> 

     private void loaded(object sender, RoutedEventArgs e) 
     { 
       var richTextBox= sender as RichTextBox; 
       Object o = XamlReader.Load(string.Format(XamlTemplate, richTextBox.Tag.ToString())); 
       var section = o as Section; 
       if (section != null) 
       { 
        richTextBox.Blocks.Clear(); 
        var tempBlocks = section.Blocks.ToList(); 
        section.Blocks.Clear(); 
        foreach (Block block in tempBlocks) 
         richTextBox.Blocks.Add(block); 
     } 

private const string XamlTemplate = "<Section xml:space=\"preserve\" HasTrailingParagraphBreakOnPaste=\"False\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"><Paragraph FontSize=\"20\" FontFamily=\"Segoe WP\" Foreground=\"#FFFFFFFF\" FontWeight=\"Normal\" FontStyle=\"Normal\" FontStretch=\"Normal\" TextAlignment=\"Left\"><Run Text=\"{0}\" /><Image Source=\"ApplicationIcon.png\" Width=\"15\" Height=\"15\"/></InlineUIContainer> </Paragraph></Section>"; 

所以我解析文本框加载事件,文本和字符串的Xaml。 XamlTemplate是带笑脸模板的硬编码文本。

我的笑脸是这样工作的,但是当我在列表框中向下滚动这些Richtextbox的数字时,滚动开始跳跃,这真的很烦人。

但是,当我将列表框项目更改为固定大小它工作正常,但我需要动态更改项目的大小,对此的任何想法?

+0

您的图片位于哪里?这看起来不是一个有效的URI。 – jv42

+0

它在应用程序的根目录 –

+0

嘿..尝试使用longlistselector而不是listbox来减少渲染问题..并让我知道状态.. – Swapnika

回答

0

我认为你的图片URI是无效的。 它应该是这样的:

<InlineUIContainer> 
    <Image Source="/YourApplication;component/ApplicationIcon.png"/> 
</InlineUIContainer> 
+0

不,这不是问题。与此同时,我发现这一点:“请注意,由Xaml属性返回的XAML字符串将不包含任何存在于内容中的UIElement对象,InlineUIContainer对象将被转换为空的Run对象。”这里:http://msdn.microsoft.com/en-us/library/ee681613(v=vs.95).aspx含义我不能通过xaml元素添加图像,所以我现在尝试一种不同的方法。无论如何感谢您的答案。 –

+0

啊,这是真的,我记得现在还在读。在我的应用程序中,我决定在视图中添加一些(半平凡的)代码来填充RichTextBox,而不是尝试对其进行数据绑定。 – jv42

+0

我编辑了我的帖子,请阅读。 –

1

尝试使用此代码。

private Regex rxForbidden = new Regex(@"[<;]", RegexOptions.IgnoreCase); 
    string[] stringArray; 

    private void richTextbox_Loaded(object sender, RoutedEventArgs e) 
    { 
     var richTextBox= sender as RichTextBox; 
     string t = richTextBox.Tag.ToString(); 
     Paragraph myParagraph = new Paragraph(); 

     if (rxForbidden.IsMatch(t)) 
     { 
      s = rxForbidden.Split(t); 
      richTextBox.Blocks.Add(myParagraph); 

      for (int i = 0; i < s.Count(); i++) 
      { 

       if (s[i] != null && s[i] != "") 
       { 
        Run txt = new Run(); 
        txt.Text = s[i]; 
        myParagraph.Inlines.Add(txt); 

       } 
       else 
       { 
        Image MyImage = new Image(); 
        MyImage.Source = new BitmapImage(new Uri("/RichTextBoxText;component/smiley_72x72.png", UriKind.Relative)); 
        MyImage.Height = 30; 
        MyImage.Width = 30; 
        InlineUIContainer MyUI = new InlineUIContainer(); 
        MyUI.Child = MyImage; 
        myParagraph.Inlines.Add(MyUI); 
       } 
      } 
      richTextBox.Blocks.Add(myParagraph); 
     } 
+0

你有没有试过这个代码..? – Swapnika