Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 清除并重新添加合并后刷新模板自定义控件_C#_Wpf_Wpf Controls_Custom Controls_Resourcedictionary - Fatal编程技术网

C# 清除并重新添加合并后刷新模板自定义控件

C# 清除并重新添加合并后刷新模板自定义控件,c#,wpf,wpf-controls,custom-controls,resourcedictionary,C#,Wpf,Wpf Controls,Custom Controls,Resourcedictionary,形势 我制作了一个自定义控件消息栏,它的样式在资源字典中定义。此控件有一个依赖属性消息,如名称所示,该消息将包含一条消息 public class MessageBar : Control { public static readonly DependencyProperty MessageProperty = DependencyProperty.Register("Message", typeof(string), typeof(MessageBar)

形势

我制作了一个
自定义控件
消息栏,它的样式在资源字典中定义。此控件有一个
依赖属性
消息,如名称所示,该消息将包含一条消息

public class MessageBar : Control
{
        public static readonly DependencyProperty MessageProperty =
            DependencyProperty.Register("Message", typeof(string), typeof(MessageBar),
                new FrameworkPropertyMetadata(string.Empty, OnMessageChanged));

        static MessageBar()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(MessageBar), new FrameworkPropertyMetadata(typeof(MessageBar)));
        }

        public string Message
        {
            get { return (string)GetValue(MessageProperty); }
            set { SetValue(MessageProperty, value); }
        }

        private static void OnMessageChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            MessageBar messageBar = (MessageBar)d;

            if (e.NewValue != null && !string.IsNullOrWhiteSpace(e.NewValue.ToString()))
            {
                messageBar.Visibility = Visibility.Visible;

                if (messageBar.textBlock == null)
                    messageBar.textBlock = messageBar.GetChildOfType<TextBlock>();

           // Lots of unnecessary code
            }
}
新样式已正确加载,并且在UI中也可见。用户界面更改正确

问题

在清除并重新添加合并的词典后,我尝试在
OnMessageChanged
方法中查找
TextBlock
类型的子词典时,就会出现问题

messageBar.textBlock = messageBar.GetChildOfType<TextBlock>();
messageBar.textBlock=messageBar.GetChildOfType();
我100%确信我的
GetChildOfType()
方法没有问题。在其他地方使用时,它可以正常工作

当我在重新添加后执行此操作时,
MessageBar
没有子元素
ChildrenCount
为0

如果未清除并重新添加合并的词典,则会找到类型为
TextBlock
的子元素。这就是我想要的

我的猜测是,在清除并重新添加后,
MessageBar
没有对样式的正确引用。正因为如此,模板没有被应用

我已经尝试过的

我已经尝试重写
MessageBar
控件的
ApplyTemplate()
OnStyleChanged()
方法。但什么都不管用

问题

如何重新加载样式,以便(方法
GetChildOfType()
可以找到
TextBlock
来在
OnMessageChanged
方法中设置消息

messageBar.textBlock = messageBar.GetChildOfType<TextBlock>();
提前谢谢


问候Loetn。

在调用之前,请尝试messageBar.ApplyTemplate()GetChildOfType@ethicallogics这就成功了。谢谢!这太容易了,我甚至都没想过。。。如果你把这个写在一个答案里,我会接受的。:)我把它作为答案写的。
Try messageBar.ApplyTemplate() before calling GetChildOfType 
Try messageBar.ApplyTemplate() before calling GetChildOfType