示例#1
0
        public static void UpdateBindingTargets(this DependencyObject dependencyObject)
        {
            foreach (DependencyObject element in dependencyObject.EnumerateVisualDescendents())
            {
                LocalValueEnumerator localValueEnumerator = element.GetLocalValueEnumerator();
                while (localValueEnumerator.MoveNext())
                {
                    BindingExpressionBase bindingExpression = BindingOperations.GetBindingExpressionBase(element, localValueEnumerator.Current.Property);
                    bindingExpression?.UpdateTarget();
                }

                var itemsControl = element as ItemsControl;
                itemsControl?.Items.Refresh();
            }
        }
示例#2
0
        private Inline ImageInlineEvaluator(Match match)
        {
            if (match == null)
            {
                throw new ArgumentNullException("match");
            }

            string      linkText  = match.Groups[2].Value;
            string      url       = match.Groups[3].Value;
            BitmapImage imgSource = null;

            try
            {
                if (!Uri.IsWellFormedUriString(url, UriKind.Absolute) && !System.IO.Path.IsPathRooted(url))
                {
                    url = System.IO.Path.Combine(AssetPathRoot ?? string.Empty, url);
                }

                imgSource = new BitmapImage(new Uri(url, UriKind.RelativeOrAbsolute));
            }
            catch (Exception)
            {
                return(new Run("!" + url)
                {
                    Foreground = Brushes.Red
                });
            }

            Image image = new Image {
                Source = imgSource, Tag = linkText
            };

            if (ImageStyle == null)
            {
                image.Margin = new Thickness(0);
            }
            else
            {
                image.Style = ImageStyle;
            }

            // Bind size so document is updated when image is downloaded
            if (imgSource.IsDownloading)
            {
                Binding binding = new Binding(nameof(BitmapImage.Width));
                binding.Source = imgSource;
                binding.Mode   = BindingMode.OneWay;

                BindingExpressionBase bindingExpression        = BindingOperations.SetBinding(image, Image.WidthProperty, binding);
                EventHandler          downloadCompletedHandler = null;
                downloadCompletedHandler = (sender, e) =>
                {
                    imgSource.DownloadCompleted -= downloadCompletedHandler;
                    bindingExpression.UpdateTarget();
                };
                imgSource.DownloadCompleted += downloadCompletedHandler;
            }
            else
            {
                image.Width = imgSource.Width;
            }

            return(new InlineUIContainer(image));
        }
示例#3
0
            /// <summary>
            /// DragEnd event handler from DragDrop behavior.
            /// </summary>
            private void SourceDoDragDrop(ITextSelection selection, IDataObject dataObject)
            {
                // Run OLE drag-drop process. It will eat all user input until the drop
                DragDropEffects allowedDragDropEffects = DragDropEffects.Copy;

                if (!_textEditor.IsReadOnly)
                {
                    allowedDragDropEffects |= DragDropEffects.Move;
                }

                DragDropEffects resultingDragDropEffects = DragDropEffects.None;

                try
                {
                    resultingDragDropEffects = DragDrop.DoDragDrop( //
                        _textEditor.UiScope,                        // dragSource,
                        dataObject,                                 //
                        allowedDragDropEffects);
                }
                // Ole32's DoDragDrop can return E_UNEXCEPTED, which comes to us as a COMException,
                // if something unexpected happened during the drag and drop operation,
                // e.g. the application receiving the drop failed. In this case we should
                // not fail, we should catch the exception and act as if the drop wasn't allowed.
                catch (COMException ex) when(ex.HResult == NativeMethods.E_UNEXPECTED)
                {
                }

                // Remove source selection
                if (!_textEditor.IsReadOnly &&                          //
                    resultingDragDropEffects == DragDropEffects.Move && //
                    _dragSourceTextRange != null &&
                    !_dragSourceTextRange.IsEmpty)
                {
                    // Normally we delete the source selection from OnDrop event,
                    // unless source and target TextBoxes are different.
                    // In this case the source selection is still not empty,
                    // which means that target was in a different TextBox.
                    // So we still need to delete the selected content in the source one.
                    // This will create an undo unit different from a dropping one,
                    // which is ok, because it will be in different TextBox's undo stack.
                    using (selection.DeclareChangeBlock())
                    {
                        // This is end of Move - we need to delete source content
                        _dragSourceTextRange.Text = String.Empty;
                    }
                }

                // Clean up the text range.
                _dragSourceTextRange = null;

                // Check the data binding expression and update the source and target if the drag source
                // has the binding expression. Without this, data binding is broken after complete the
                // drag-drop operation because Drop() paste the object then set the focus to the target.
                // The losting focus invoke the data binding expression's Update(), but the source isn't
                // updated yet before complete DoDragDrop.
                if (!_textEditor.IsReadOnly)
                {
                    BindingExpressionBase bindingExpression = BindingOperations.GetBindingExpressionBase(
                        _textEditor.UiScope, TextBox.TextProperty);
                    if (bindingExpression != null)
                    {
                        bindingExpression.UpdateSource();
                        bindingExpression.UpdateTarget();
                    }
                }
            }
示例#4
0
        private Inline ImageInlineEvaluator(Match match)
        {
            if (match is null)
            {
                throw new ArgumentNullException(nameof(match));
            }

            string      linkText  = match.Groups[2].Value;
            string      url       = match.Groups[3].Value;
            BitmapImage imgSource = null;

            try
            {
                if (!Uri.IsWellFormedUriString(url, UriKind.Absolute) && !System.IO.Path.IsPathRooted(url))
                {
                    url = System.IO.Path.Combine(AssetPathRoot ?? string.Empty, url);
                }

                imgSource = new BitmapImage();
                imgSource.BeginInit();
                imgSource.CacheOption    = BitmapCacheOption.None;
                imgSource.UriCachePolicy = new RequestCachePolicy(RequestCacheLevel.BypassCache);
                imgSource.CacheOption    = BitmapCacheOption.OnLoad;
                imgSource.CreateOptions  = BitmapCreateOptions.IgnoreImageCache;
                imgSource.UriSource      = new Uri(url);
                imgSource.EndInit();
            }
            catch (Exception)
            {
                return(new Run("!" + url)
                {
                    Foreground = Brushes.Red
                });
            }

            Image image = new Image {
                Source = imgSource, Tag = linkText
            };

            if (ImageStyle is null)
            {
                image.Margin = new Thickness(0);
            }
            else
            {
                image.Style = ImageStyle;
            }

            // Bind size so document is updated when image is downloaded
            if (imgSource.IsDownloading)
            {
                Binding binding = new Binding(nameof(BitmapImage.Width));
                binding.Source = imgSource;
                binding.Mode   = BindingMode.OneWay;

                BindingExpressionBase bindingExpression = BindingOperations.SetBinding(image, Image.WidthProperty, binding);

                void downloadCompletedHandler(object sender, EventArgs e)
                {
                    imgSource.DownloadCompleted -= downloadCompletedHandler;
                    bindingExpression.UpdateTarget();
                }

                imgSource.DownloadCompleted += downloadCompletedHandler;
            }
            else
            {
                image.Width = imgSource.Width;
            }

            return(new InlineUIContainer(image));
        }
示例#5
0
 protected override void OnClick()
 {
     base.OnClick();
     isCheckedBinding.UpdateTarget();
 }
示例#6
0
        private Inline ImageInlineEvaluator(Match match)
        {
            if (match == null)
            {
                throw new ArgumentNullException("match");
            }

            string      linkText  = match.Groups[2].Value;
            string      url       = match.Groups[3].Value;
            BitmapImage imgSource = null;
            Image       image     = null;

            try {
                string assembly_name = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
                //url = url.Replace('/', '\\');
                string resource_url = $"pack://application:,,,/" + url;  // https://stackoverflow.com/questions/12954327/dynamically-adding-and-loading-image-from-resources-in-c-sharp
                Uri    resource_uri = new Uri(resource_url);

                // Загрузить изображение надо в любом случае т.к. надо узнать его ширину ниже.
                BitmapImage bi = new BitmapImage();
                bi.BeginInit();
                bi.UriSource = resource_uri;
                bi.EndInit();
                imgSource = bi;

                //Binding b = new Binding("ActualWidth");

                // Анимированные gif-ки надо подгружать немного иначе через компонент XamlAnimatedGif:
                if (System.IO.Path.GetExtension(url).ToLower().Equals(".gif") == true)
                {
                    image = new Image()
                    {
                        Stretch = Stretch.UniformToFill                                         /*, MaxWidth=500*/
                    };
                    XamlAnimatedGif.AnimationBehavior.SetSourceUri(image, imgSource.UriSource); // https://wpfanimatedgif.codeplex.com/documentation
                }
                else
                {
                    image = new Image {
                        Source = imgSource, Tag = linkText, Stretch = Stretch.UniformToFill               /*, MaxWidth = 500*/
                    };
                }

                //if (!Uri.IsWellFormedUriString(url, UriKind.Absolute) && !System.IO.Path.IsPathRooted(url)) {
                //    url = System.IO.Path.Combine(AssetPathRoot ?? string.Empty, url);
                //}

                //imgSource = new BitmapImage(new Uri(url, UriKind.RelativeOrAbsolute));
            }
            catch (Exception) {
                return(new Run("!" + url)
                {
                    Foreground = Brushes.Red
                });
            }

            if (ImageStyle == null)
            {
                image.Margin = new Thickness(0);
            }
            else
            {
                image.Style = ImageStyle;
            }

            // Bind size so document is updated when image is downloaded
            if (imgSource.IsDownloading)
            {
                Binding binding = new Binding(nameof(BitmapImage.Width));
                binding.Source = imgSource;
                binding.Mode   = BindingMode.OneWay;

                BindingExpressionBase bindingExpression        = BindingOperations.SetBinding(image, Image.WidthProperty, binding);
                EventHandler          downloadCompletedHandler = null;
                downloadCompletedHandler = (sender, e) =>
                {
                    imgSource.DownloadCompleted -= downloadCompletedHandler;
                    bindingExpression.UpdateTarget();
                };
                imgSource.DownloadCompleted += downloadCompletedHandler;
            }
            else
            {
                //image.Width = imgSource.Width;
                //image.SnapsToDevicePixels = true;
                image.Width = imgSource.PixelWidth;
            }
            InlineUIContainer iuc = new InlineUIContainer(image);

            return(iuc);
        }