TemplateBinding not working for textbox text

后端 未结 2 1856
半阙折子戏
半阙折子戏 2021-01-12 03:59

I have a custom control called EnhancedTextBox which is a UserControl that has a TextBox and a Button. To the consumer I want it to mo

相关标签:
2条回答
  • 2021-01-12 04:30

    I figured it out after reading this MSDN about TemplateBinding. Specifically,

    A TemplateBinding is an optimized form of a Binding for template scenarios, analogous to a Binding constructed with {Binding RelativeSource={RelativeSource TemplatedParent}}.

    So, I decided to do this explicitly...which would allow me to set the UpdateSourceTrigger (still not sure why it doesn't default to PropertyChanged)

    <TextBox Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Text, UpdateSourceTrigger=PropertyChanged}"....
    

    And, now it is working. TemplateBinding does not even expose these properties....again, not sure why

    0 讨论(0)
  • 2021-01-12 04:30

    You are missing the CallBack when you register the property.

    Here's a sample code.

        public bool IsSelected
        {
            get { return (bool)GetValue(IsSelectedProperty); }
            set { SetValue(IsSelectedProperty, value); }
        }
    
        public void IsSelectedChangedCallback()
        {
    
            //actions when property changed
    
        }
    
        private static void OnSelectedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            userControl.IsSelectedChangedCallback();
        }
    
        public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.Register("IsSelected", typeof(bool), typeof(MyUserControl), new PropertyMetadata(new PropertyChangedCallback(OnSelectedChanged)));
    
    0 讨论(0)
提交回复
热议问题