public static void SafeBeginInit(this FrameworkElement reference) { if (reference.CheckAccess()) { reference.BeginInit(); } else { reference.Dispatcher.Invoke(new Action(delegate { reference.BeginInit(); })); } }
private void LoadForm(string form) { StreamReader mysr = new StreamReader(form); FrameworkElement rootObject = XamlReader.Load(mysr.BaseStream) as FrameworkElement; for (int radioButtons = 0; radioButtons < 7; radioButtons++) { RadioButton rButton = LogicalTreeHelper.FindLogicalNode(rootObject, "R" + radioButtons) as RadioButton; if (rButton != null) { rButton.Click += UpdateValidation; } } display.Children.Clear(); display.Children.Add(rootObject); rootObject.BeginInit(); display.DataContext = Respondent; //rootObject.Measure(renderingSize); //rootObject.Arrange(renderingRectangle); rootObject.EndInit(); rootObject.UpdateLayout(); }
private void SetElemVisible(FrameworkElement elem) { if (elem.Visibility == System.Windows.Visibility.Visible) { return; } mainGrid.BeginInit(); List <FrameworkElement> toInvis = new List <FrameworkElement>(); if (elem == lstResView) { toInvis.Add(leadPhoto); } else if (elem == leadPhoto) { toInvis.Add(lstResView); } foreach (var e in toInvis) { e.BeginInit(); if (e.Visibility == System.Windows.Visibility.Visible && backupRow != null) { Grid.SetRow(e, backupRow.Value); backupRow = null; } e.Visibility = System.Windows.Visibility.Collapsed; e.EndInit(); } elem.BeginInit(); backupRow = Grid.GetRow(elem); Grid.SetRow(elem, 1); elem.Visibility = System.Windows.Visibility.Visible; elem.EndInit(); mainGrid.EndInit(); }
private static void ClearDimensions(FrameworkElement tabItem) { // remove any size restrictions from the Header, // this is because the TabControl's size restriction properties takes precedence over // the individual tab items // eg, if the TabControl sets the TabItemMaxWidth property to 300, but the Header // has a minWidth of 400, the TabControls value of 300 should be used tabItem.BeginInit(); tabItem.Height = double.NaN; tabItem.Width = double.NaN; tabItem.MaxHeight = double.PositiveInfinity; tabItem.MaxWidth = double.PositiveInfinity; tabItem.MinHeight = 0; tabItem.MinWidth = 0; tabItem.EndInit(); }
private void InsertAdorners() { if (VisualTreeHelper.GetChildrenCount(AssociatedObject) == 0) { return; } AssociatedObject.LayoutUpdated -= AssociatedObjectLayoutUpdated; _beginInt = AssociatedObject.IsInitialized; if (!_beginInt) { AssociatedObject.BeginInit(); } // // Create visual for adorners // FrameworkElement topVisuals = TopTemplate.LoadContent() as FrameworkElement; if (topVisuals != null) { var topBorder = topVisuals.FindName("AdornerBorder") as FrameworkElement; if (topBorder != null) { var behaviors = Interaction.GetBehaviors(AssociatedObject); var actualSizeBehavior = behaviors.FirstOrDefault(b => b.GetType() == typeof(ActualSizeBindingBehavior)) as ActualSizeBindingBehavior; if (actualSizeBehavior != null) { var widthBinding = new Binding { Source = actualSizeBehavior, Path = new PropertyPath("ActualWidth"), Converter = new DoubleToMarginLeftConverter() }; topBorder.SetBinding(FrameworkElement.MarginProperty, widthBinding); } } } if (topVisuals != null && DataContext != null) { topVisuals.DataContext = DataContext; } // // Setup intercept for supressing the connector nodes // _rootAdornerLayer = AdornerLayer.GetAdornerLayer(AssociatedObject); if (_rootAdornerLayer != null) { _rootAdornerLayer.LayoutUpdated += AdornerLayerOnMouseLeave; } // // Get controls from visual tree // Border childBorder = VisualTreeHelper.GetChild(AssociatedObject, 0) as Border; if (childBorder == null) { return; } Grid hostGrid = childBorder.Child as Grid; if (hostGrid == null) { return; } Border titleBarBorder = GetColoursBorder(hostGrid); if (titleBarBorder == null) { return; } // // Set colour attached properties // Binding backgroundBinding = new Binding("Background"); backgroundBinding.Source = titleBarBorder; Binding borderBrushBinding = new Binding("BorderBrush"); borderBrushBinding.Source = titleBarBorder; if (topVisuals != null) { topVisuals.BeginInit(); topVisuals.SetBinding(TitleBarBackgroundProperty, backgroundBinding); topVisuals.SetBinding(TitleBarBorderBrushProperty, borderBrushBinding); topVisuals.EndInit(); } // // Insert adorners // AdornerDecorator decorator = new AdornerDecorator(); childBorder.Child = null; decorator.Child = hostGrid; childBorder.Child = decorator; AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(hostGrid); if (!_beginInt) { AssociatedObject.BeginInit(); } TopVisualsAdornerWrapper topVisualsAdornerWrapper = new TopVisualsAdornerWrapper(hostGrid, topVisuals, AssociatedObject); adornerLayer.Add(topVisualsAdornerWrapper); if (!_beginInt) { adornerLayer.EndInit(); AssociatedObject.EndInit(); } AdornerToggleButtons.Clear(); topVisualsAdornerWrapper.Descendents().OfType <AdornerToggleButton>().ToList() .ForEach(d => { AdornerToggleButtons.Add(d); d.Click += d_Click; }); }
/// <summary> /// Measure the tab items for docking at the left or right /// </summary> private Size MeasureVertical(Size availableSize) { ItemsControl ic = ItemsControl.GetItemsOwner(this); if (ic == null || ic.Items.Count == 0) { return(new Size()); } _maxChildWidthOrHeight = 0; _actualChildCount = ic.Items.Count; EnsureChildRects(_actualChildCount); double extentHeight = 0; double[] heights = new double[ic.Items.Count]; // we will first measure all the children with unlimited space to get their desired sizes // this will also get us the height required for all TabItems for (int i = 0; i < ic.Items.Count; i++) { FrameworkElement child = ic.Items[i] as FrameworkElement; if (child == null) { return(new Size()); } // reset any width/height constraints on the child, // as the autosizing function does not support them child.BeginInit(); child.Width = double.NaN; child.MaxWidth = double.PositiveInfinity; child.MinWidth = 0; child.Height = double.NaN; child.MaxHeight = double.PositiveInfinity; child.MinHeight = 0; child.EndInit(); child.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); // calculate the maximum child width _maxChildWidthOrHeight = Math.Max(_maxChildWidthOrHeight, Math.Ceiling(child.DesiredSize.Width)); // calculate the child width while respecting the Maximum & Minimum width constraints heights[i] = Math.Min(MaximumChildHeight, Math.Max(MinimumChildHeight, Math.Ceiling(child.DesiredSize.Height))); // determines how much horizontal space we require extentHeight += heights[i]; } _maxChildWidthOrHeight = Math.Max(MinimumChildWidth, Math.Min(MaximumChildWidth, _maxChildWidthOrHeight)); // observe the constraints _extent = new Size(_maxChildWidthOrHeight, extentHeight); bool flag = false; // 1). all the children fit into the available space using there desired widths if (extentHeight <= availableSize.Height) { _maxVisibleItems = ic.Items.Count; FirstVisibleIndex = 0; double top = 0; for (int i = 0; i < _actualChildCount; i++) { _childRects[i] = new Rect(0, top, _maxChildWidthOrHeight, heights[i]); top += heights[i]; } CanScrollLeftOrUp = false; CanScrollRightOrDown = false; flag = true; } // 2). all the children fit in the available space if we reduce their widths to a uniform value // while staying within the MinimumChildWidth and MaximumChildWidth constraints if (!flag) { // make sure the width is not greater than the MaximumChildWidth constraints double targetHeight = Math.Min(MaximumChildHeight, availableSize.Height / _actualChildCount); // target width applies now if whether we can fit all items in the available space or whether we are scrolling if (targetHeight >= MinimumChildHeight) { _maxVisibleItems = ic.Items.Count; FirstVisibleIndex = 0; extentHeight = 0; double top = 0; for (int i = 0; i < _actualChildCount; i++) { extentHeight += targetHeight; heights[i] = targetHeight; _childRects[i] = new Rect(0, top, _maxChildWidthOrHeight, heights[i]); top += heights[i]; } _extent = new Size(_maxChildWidthOrHeight, extentHeight); flag = true; CanScrollLeftOrUp = false; CanScrollRightOrDown = false; } } // 3) we can not fit all the children in the viewport, so now we will enable scrolling/virtualizing items if (!flag) { _maxVisibleItems = (int)Math.Floor(_viewPort.Height / MinimumChildHeight); // calculate how many visible children we can show at once double targetHeight = availableSize.Height / _maxVisibleItems; // calculate the new target width FirstVisibleIndex = _firstVisibleIndex; extentHeight = 0; double top = 0; for (int i = 0; i < _actualChildCount; i++) { extentHeight += targetHeight; heights[i] = targetHeight; _childRects[i] = new Rect(0, top, _maxChildWidthOrHeight, heights[i]); top += heights[i]; } _extent = new Size(_maxChildWidthOrHeight, extentHeight); CanScrollLeftOrUp = LastVisibleIndex < _actualChildCount - 1; CanScrollRightOrDown = FirstVisibleIndex > 0; } UIElementCollection children = this.InternalChildren; IItemContainerGenerator generator = this.ItemContainerGenerator; // Realize the visible items GeneratorPosition pos = generator.GeneratorPositionFromIndex(FirstVisibleIndex); int childIndex = (pos.Offset == 0) ? pos.Index : pos.Index + 1; using (generator.StartAt(pos, GeneratorDirection.Forward, true)) { for (int itemIndex = FirstVisibleIndex; itemIndex <= LastVisibleIndex; itemIndex++, ++childIndex) { bool newlyRealized = false; UIElement child = generator.GenerateNext(out newlyRealized) as UIElement; if (newlyRealized) { if (childIndex >= children.Count) { base.AddInternalChild(child); } else { base.InsertInternalChild(childIndex, child); } generator.PrepareItemContainer(child); } child.Measure(new Size(_maxChildWidthOrHeight, heights[itemIndex])); } } // virtualize items that are no longer visible CleanUpItems(); return(new Size(_maxChildWidthOrHeight, availableSize.Height)); }
public static FixedDocumentSequence GetDocumentSequence(object dataContext, Stream inputStream, Size paperSize) { //承载一个可移植、高保真、格式固定的文档。 FixedDocument _document = new FixedDocument(); //为高保真度、固定格式的页面提供内容。 FixedPage page = new FixedPage(); //高度 page.Height = paperSize.Height; //宽度 page.Width = paperSize.Width; //提供有关FixedDocument中的FixedPage元素的信息。 PageContent content = new PageContent(); //Child:获取或设置与此PageContent关联的FixedPage。 content.Child = page; //属性Pages:获取文档的PageContent元素的集合。 //Add方法:向页集合中添加新页。 _document.Pages.Add(content); FrameworkElement template = (FrameworkElement)XamlReader.Load(inputStream); template.BeginInit(); template.DataContext = dataContext; template.Measure(paperSize); template.Arrange(new Rect(paperSize)); template.EndInit(); template.UpdateLayout(); page.Children.Add(template); //构造一个基于内存的xps document 为什么? MemoryStream ms = new MemoryStream(); //为XPS内容创建包 Package package = Package.Open(ms, FileMode.Create, FileAccess.ReadWrite); //每个包都需要一个URI。形式:"pack:// syntax" Uri documentUri = new Uri("pack://InMemoryDocument.xps"); PackageStore.RemovePackage(documentUri); //加入该包 PackageStore.AddPackage(documentUri, package); //基于该包创建XPS的XpsDocument对象 XpsDocument xpsDocument = new XpsDocument(package, CompressionOption.Fast, documentUri.AbsoluteUri); //将flow document写入基于内存的xps document中去 XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDocument); //分页 writer.Write(((IDocumentPaginatorSource)_document).DocumentPaginator); //获取这个基于内存的xps document的fixed document FixedDocumentSequence pages = xpsDocument.GetFixedDocumentSequence(); //关闭基于内存的xps document 关闭流以释放内存 xpsDocument.Close(); return(pages); }