How to create a speech bubble in UWP?

后端 未结 2 1113
别那么骄傲
别那么骄傲 2020-12-10 18:00

I\'m creating a chat application and would like to create the typical speech bubble that contains each message. I created a Path object in Blend (in XAML) like this:

2条回答
  •  轮回少年
    2020-12-10 18:28

    Here is a Custom control that declares a Dependency Property for the Text and reuse some properties of the base control in its template (Background, Width, Heigth).

    First the class definition: (SpeechBubbleControl.xaml.cs)

    [TemplatePart(Name = PartBubbleText, Type = typeof(TextBlock))]
    public sealed partial class SpeechBubbleControl : Control
    {
        private const string PartBubbleText = "BubbleText";
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register(nameof(Text), typeof(string), typeof(SpeechBubbleControl), new PropertyMetadata(""));
    
        public SpeechBubbleControl()
        {
            DefaultStyleKey = typeof(SpeechBubbleControl);
        }
    
        public string Text
        {
            get { return GetValue(TextProperty).ToString(); }
            set { SetValue(TextProperty, value); }
        } 
    }
    

    With its default template (SpeechBubbleControl.xaml):

    
    
        
    
    

    You have to import this resource into your application resource using something like this in your app.xaml:

    
        
            
                
                    
                
            
        
    
    

    And finally a sample test page, which use this control with bindings on width, height (based on the sliders) and the text that must be displayed.

    
            
                
                
            
            
                
                
                
            
            
            
            
    
            
            
        
    
        
    
        
    
    

    Here is the result:

    Note that my answer is adapted from this one: WPF speech bubble

提交回复
热议问题