2017-06-23 54 views
1

我想添加表情符号到WPF聊天应用程序。我知道wpf不支持表情符号,因此我用图像替换表情符号。我正在使用textBlock的内联属性将图像添加到textBlock,但是,我遇到了图像对齐的问题。我无法使表情图像正确对齐。我分享了它如何看的截图。WPF应用程序TextBlock中的表情符号

Screenshot of app window

This is how emoticon is looking

的例子就是在那里,我将在构造函数中的元素只看到它如何看一个演示。我也在分享我的代码。

 @out.Inlines.Add(new Run("Hii, my name is Ajay!!")); 
     Image emo = new Image(); 
     emo.Height = 15; 
     emo.Width = 15; 
     emo.VerticalAlignment = VerticalAlignment.Bottom; 
     emo.Margin = new Thickness(3, 0, 0, 0); 
     emo.Source = new BitmapImage(new Uri(@"C:\Users\admin\Desktop\test1.jpg", UriKind.RelativeOrAbsolute)); 
    // InlineUIContainer container = new InlineUIContainer(emo); 
     @out.Inlines.Add(emo); 

有没有什么办法可以使表情图像正确对齐?是否可以使用textblock或者我应该使用任何其他控件?

任何帮助,高度赞赏。

+0

您能否澄清一下您的意思是否正确对齐?你想让图片出现在右侧吗? –

回答

1

有几个潜在的选择可能是:

设置Top缘上的图像。它在左,上,右的格式,BOTTOM

emo.Margin = new Thickness(3, 4, 0, 0); 

另一种选择是包裹在运行的图像,并设置BaselineAlignmenthttps://msdn.microsoft.com/en-us/library/system.windows.baselinealignment

var imageRun= new Run(emo); 
imageRun.BaselineAlignment = BaselineAlignment.TextBottom; //experiment with the other enum options 
@out.Inlines.Add(imageRun); 

调整文本而不是图像(虽然我会保持与图像尝试,并以此作为最后的手段)。

var textRun = new Run("Hii, my name is Ajay!!"); 
textRun.Margin = experiment; 
textRun.BaselineAlignment = experiment; 
@out.Inlines.Add(textRun); 
+0

实际上,运行的构造函数并不以UIElement为参数,但是,我试图为textRun设置BaselineAlignment,它运行得非常好! –

+0

非常感谢! :) –

1

我试了@Bill Tarbell的建议,它为我工作。 最后的工作代码如下:

 var textRun = new Run("Hii, my name is Ajay!!"); 
     textRun.BaselineAlignment = BaselineAlignment.Center; 
     @out.Inlines.Add(textRun); 
     Image emo = new Image(); 
     emo.Height = 20; 
     emo.Width = 20; 
     emo.VerticalAlignment = VerticalAlignment.Bottom; 
     emo.Margin = new Thickness(3, 0, 0, 0); 
     emo.Source = new BitmapImage(new Uri(@"C:\Users\admin\Desktop\test1.jpg", UriKind.RelativeOrAbsolute)); 
     // InlineUIContainer container = new InlineUIContainer(emo); 
     @out.Inlines.Add(emo) 
相关问题