Why does my adorner not re-render when the element it's applied to changes?

后端 未结 2 1050
梦谈多话
梦谈多话 2021-02-01 06:32

In a UI I\'m building, I want to adorn a panel whenever one of the controls in the panel has the focus. So I handle the IsKeyboardFocusWithinChanged event, and add

2条回答
  •  离开以前
    2021-02-01 07:26

    You need to invoke the dispatcher on the panel. Add a handler to the TextBox SizeChanged event:

        private void myTextBox_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            panel.Dispatcher.Invoke((Action)(() => 
            {
                if (panel.IsKeyboardFocusWithin)
                {
                    // remove and add adorner to reset
                    myAdornerLayer.Remove(myAdorner);
                    myAdornerLayer.Add(myAdorner);
                }
            }), DispatcherPriority.Render, null);
        }
    

    This basically comes from this post: http://geekswithblogs.net/NewThingsILearned/archive/2008/08/25/refresh--update-wpf-controls.aspx

提交回复
热议问题