RadComboBox with Floating Label same as a RadWatermarkTextBox?

1 Answer 247 Views
ComboBox WatermarkTextBox
Avrohom
Top achievements
Rank 1
Iron
Iron
Avrohom asked on 03 Jun 2022, 10:59 AM

Hi,

Would it be possible to have a editable RadComboBox TextBox Part to mimic the RadWatermarkTextBox with floating Label?

1 Answer, 1 is accepted

Sort by
0
Accepted
Avrohom
Top achievements
Rank 1
Iron
Iron
answered on 08 Jun 2022, 01:16 AM

Got it.

 


    public class WatermarkComboBox : RadComboBox
    {
        private RadWatermarkTextBox _waterTb;
        public override void OnApplyTemplate()
        {
            TextBox PART_EditableTextBox = Template.FindName("PART_EditableTextBox", this) as TextBox;
            Panel p = (Panel)System.Windows.Media.VisualTreeHelper.GetParent(PART_EditableTextBox);
            p.Children.Remove(PART_EditableTextBox);
            _waterTb = new RadWatermarkTextBox() { Name = "PART_EditableTextBox" };
            MethodInfo methodToReplace = typeof(RadComboBox).GetMethod("GetTemplateChild", BF.NonPublic | BF.Instance);
            MethodInfo methodToInject = this.GetType().GetMethod("GetTemplateChild1", BF.Public | BF.Instance);
            Install(methodToReplace, methodToInject);
            p.Children.Add(_waterTb);
            var binding = new Binding { Source = this, Path = new PropertyPath(LabelProperty) };
            _waterTb.SetBinding(RadWatermarkTextBox.LabelProperty, binding);
            base.OnApplyTemplate();
        }

        public DependencyObject GetTemplateChild1(string childName)
        {
            if (childName == "PART_EditableTextBox")
                return _waterTb;
            return (DependencyObject)Template.FindName(childName, this);
        }

        // Credit: https://stackoverflow.com/a/36415711/1554806
        public static void Install(MethodInfo methodToReplace, MethodInfo methodToInject)
        {
            RuntimeHelpers.PrepareMethod(methodToReplace.MethodHandle);
            RuntimeHelpers.PrepareMethod(methodToInject.MethodHandle);

            unsafe
            {
                if (IntPtr.Size == 4)
                {
                    int* inj = (int*)methodToInject.MethodHandle.Value.ToPointer() + 2;
                    int* tar = (int*)methodToReplace.MethodHandle.Value.ToPointer() + 2;
#if DEBUG
                    System.Diagnostics.Debug.WriteLine("\nVersion x86 Debug\n");

                    byte* injInst = (byte*)*inj;
                    byte* tarInst = (byte*)*tar;

                    int* injSrc = (int*)(injInst + 1);
                    int* tarSrc = (int*)(tarInst + 1);

                    *tarSrc = (((int)injInst + 5) + *injSrc) - ((int)tarInst + 5);
#else
                    System.Diagnostics.Debug.WriteLine("\nVersion x86 Release\n");
                    *tar = *inj;
#endif
                }
                else
                {

                    long* inj = (long*)methodToInject.MethodHandle.Value.ToPointer() + 1;
                    long* tar = (long*)methodToReplace.MethodHandle.Value.ToPointer() + 1;
#if DEBUG
                    System.Diagnostics.Debug.WriteLine("\nVersion x64 Debug\n");
                    byte* injInst = (byte*)*inj;
                    byte* tarInst = (byte*)*tar;


                    int* injSrc = (int*)(injInst + 1);
                    int* tarSrc = (int*)(tarInst + 1);

                    *tarSrc = (((int)injInst + 5) + *injSrc) - ((int)tarInst + 5);
#else
                    System.Diagnostics.Debug.WriteLine("\nVersion x64 Release\n");
                    *tar = *inj;
#endif
                }
            }
        }

        public string Label
        {
            get { return (string)GetValue(LabelProperty); }
            set { SetValue(LabelProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Label.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty LabelProperty =
            DependencyProperty.Register("Label", typeof(string), typeof(WatermarkComboBox), new PropertyMetadata(null));
        
        }
    }

Tags
ComboBox WatermarkTextBox
Asked by
Avrohom
Top achievements
Rank 1
Iron
Iron
Answers by
Avrohom
Top achievements
Rank 1
Iron
Iron
Share this question
or