コード例 #1
0
        void OnContentChanged()
        {
            if (_contentPresenter == null)
            {
                throw new InvalidOperationException("'ContentPresenter' element not found.");
            }

            _isContentChanging = true;

            var content = Content;

            if (content == null)
            {
                _isContentChanging          = false;
                _contentPresenter.InnerHTML = null;
                return;
            }

            var frameworkElement = content as FrameworkElement;

            if (frameworkElement != null)
            {
                if (_contentPresenter.LogicalChilderenCount == 1)
                {
                    _contentPresenter.RemoveLogicalChild(_contentPresenter.GetLogicalChildAt(0));
                }

                if (_contentPresenter.VisualChilderenCount == 1)
                {
                    _contentPresenter.RemoveVisualChild(_contentPresenter.GetVisualChildAt(0));
                }

                _contentPresenter.AddLogicalChild(frameworkElement);

                if (LogicalChilderenCount == 1)
                {
                    RemoveLogicalChild(GetLogicalChildAt(0));
                }

                AddLogicalChild(frameworkElement);

                _isContentChanging = false;
                return;
            }

            var contentAsString = content.ToString();

            var textBlock = UIBuilder.Create <TextBlock>();

            textBlock.Text = contentAsString;
            _contentPresenter.AddLogicalChild(textBlock);

            if (LogicalChilderenCount == 1)
            {
                RemoveLogicalChild(GetLogicalChildAt(0));
            }

            AddLogicalChild(textBlock);

            _isContentChanging = false;
        }