public PopupButton() { var content = new ContentPresenter(); content.SetBinding(ContentPresenter.ContentProperty, new Binding("PopupContent") { Source = this }); var border = new Border() { CornerRadius = new CornerRadius(5), BorderThickness = new Thickness(1), Child = content }; border.SetResourceReference(Border.BackgroundProperty, "BaseWindowBackgroundBrush"); border.SetResourceReference(Border.BorderBrushProperty, "WindowBorderBrush"); _popup = new Popup() { AllowsTransparency = true, StaysOpen = false, Placement = PlacementMode.Bottom, PlacementTarget = this, DataContext = this, Child = border, }; _popup.SetBinding(Popup.IsOpenProperty, "IsChecked"); _popup.SetBinding(Popup.WidthProperty, "Width"); SetBinding(PopupButton.IsHitTestVisibleProperty, new Binding("IsOpen") { Source = _popup, Mode = BindingMode.OneWay, Converter = new InverseBooleanConverter() }); }
public ThemedWindow() { this.ShouldBeThemed(); WindowStyle = WindowStyle.None; ResizeMode = ResizeMode.CanResizeWithGrip; Background = Brushes.Transparent; AllowsTransparency = true; WindowStartupLocation = WindowStartupLocation.CenterOwner; ShowInTaskbar = false; Grid host = new Grid(); //Header host.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); //Body host.RowDefinitions.Add(new RowDefinition()); FrameworkElement header = BuildHeaderArea(); header.SetValue(Grid.RowProperty, 0); host.Children.Add(header); ContentPresenter contentPresenter = new ContentPresenter(); contentPresenter.SetValue(Grid.RowProperty, 1); contentPresenter.SetBinding(ContentPresenter.ContentProperty, new Binding { Mode = BindingMode.OneWay, RelativeSource = new RelativeSource { Mode = RelativeSourceMode.FindAncestor, AncestorType = typeof(ThemedWindow) }, Path = new PropertyPath("Content") }); contentPresenter.Resources = Resources; host.Children.Add(contentPresenter); host.SetResourceReference(BackgroundProperty, EnvironmentColors.ToolWindowBackgroundBrushKey); Border hostContainer = new Border { Child = host, //Margin = new Thickness(1, 1, 5, 5), BorderThickness = new Thickness(1) }; hostContainer.SetResourceReference(BorderBrushProperty, EnvironmentColors.MainWindowActiveDefaultBorderBrushKey); //hostContainer.Effect = new DropShadowEffect //{ // Direction = -75, // ShadowDepth = 2, // BlurRadius = 2, // Color = Colors.Azure //}; base.Content = hostContainer; }
protected override FrameworkElement GenerateEditingElementCore() { var control = new ContentPresenter { ContentTemplate = this.EditingTemplate }; control.SetBinding(ContentPresenter.ContentProperty, this.Binding); return control; }
public void RealizeContent() { if (_contentPresenter != null) { return; // throw new InvalidOperationException("Already realized!"); // probably want to not throw in the future, but just no-op out? } Debug("RealizeContent"); _contentPresenter = new ContentPresenter { HorizontalAlignment = HorizontalContentAlignment, VerticalAlignment = VerticalContentAlignment, Margin = Padding, IsHitTestVisible = false, }; _contentPresenter.Loaded += OnContentPresenterLoaded; _contentPresenter.Unloaded += OnContentPresenterUnloaded; _contentPresenter.SetBinding( ContentPresenter.ContentTemplateProperty, new Binding("ContentTemplate") { BindsDirectlyToSource = true, Source = this }); _contentPresenter.SetBinding( ContentPresenter.ContentProperty, new Binding("Content") { BindsDirectlyToSource = true, Source = this }); _contentPanel.Children.Add(_contentPresenter); _contentPanel.Visibility = Visibility.Visible; // ? is this one needed ? }
public void BindContentPresenterContent () { ContentPresenter presenter = new ContentPresenter (); presenter.SetBinding (ContentPresenter.ContentProperty, new Binding ("Opacity")); CustomControl c = new CustomControl { Content = presenter }; CreateAsyncTest (c, () => { c.DataContext = new Data { Opacity = 1.0 }; }, () => { Assert.AreEqual (1.0, presenter.ReadLocalValue (ContentPresenter.DataContextProperty), "#1"); Assert.AreEqual (1.0, presenter.Content, "#2"); c.DataContext = new Data { Opacity = 0.0 }; }, () => { Assert.AreEqual (0.0, presenter.ReadLocalValue (ContentPresenter.DataContextProperty), "#3"); Assert.AreEqual (0.0, presenter.Content, "#4"); } ); }
private void AddTickmark(double position) { ContentPresenter c = new ContentPresenter(); c.SetValue(PositionProperty, position); c.SetBinding(ContentPresenter.ContentTemplateProperty, new System.Windows.Data.Binding() { Source = this, BindsDirectlyToSource = true, Path = new PropertyPath("TickMarkTemplate") }); Children.Add(c); }
public DialogBase() { // You might ask yourself why this is happening... // // It's all because WPF apparently has a bug where if you use the Template property on Window to customize the // visual tree of its content (i.e., put the button tray / progressive disclosure / footnote stuff in), everything // works great *except* you don't get focus visuals for any of the controls. // // So to get around that, we don't mess with the template of the window itself, but instead: // 1) Programmatically create a ContentPresenter and set it as the content of the Window (dialog) // 2) Change the default content property to be "MainContent" (so the xaml for DialogBase derivations looks "normal") // 3) Bind the content presenter to the MainContent property (and the template to MainContentTemplate) // // Viola! Plus the dialogs are now individually customizable via MainContentTemplate. var content = new ContentPresenter() { Focusable = false, Content = new SelfWrapper(this) }; content.SetBinding(ContentPresenter.ContentTemplateProperty, new Binding { Source = this, Path = new PropertyPath(MainContentTemplateProperty) }); this.Content = content; this.buttons = new ObservableCollection<Button>(); this.Loaded += OnLoaded; }
private void UnmaskHiddenContent() { if (_contentGrid == null) { #if DEBUG throw new InvalidOperationException("Content grid must be present."); #endif return; } if (_contentPresenter != null) { #if DEBUG throw new InvalidOperationException("Unmasking cannot happen twice."); #endif return; } //_hasUnmaskedContent = true; var contentBinding = new Binding( "Content") { BindsDirectlyToSource = true, Source = this }; var templateBinding = new Binding("ContentTemplate") { BindsDirectlyToSource = true, Source = this }; _contentPresenter = new ContentPresenter { HorizontalAlignment = HorizontalContentAlignment, VerticalAlignment = VerticalContentAlignment, Margin = Padding, IsHitTestVisible = false, }; _contentPresenter.Loaded += OnContentPresenterLoaded; _contentPresenter.Unloaded += OnContentPresenterUnloaded; _contentPresenter.SetBinding(ContentPresenter.ContentTemplateProperty, templateBinding); _contentPresenter.SetBinding(ContentPresenter.ContentProperty, contentBinding); _contentGrid.Children.Add(_contentPresenter); _contentGrid.Visibility = Visibility.Visible; _okToShowAfter = DateTime.Now + MinimumLoadingTime; _timeoutAfter = DateTime.Now + TimeoutSpan; // probably don't need this one if (_timeoutTimer == null) { _timeoutTimer = new DispatcherTimer(); _timeoutTimer.Interval = TimeoutSpan; _timeoutTimer.Tick += OnTimeoutTimerTick; } _timeoutTimer.Start(); }
//static void lb_MouseDown(object sender, MouseButtonEventArgs e) //{ // Adorner ad = sender as Adorner; // FrameworkElement fe = ad.AdornedElement as FrameworkElement; // fe.RaiseEvent(e); //} private static void ListBox_MouseEnter(object sender, MouseEventArgs e) { // Check that we are hovering on a ListBoxItem FrameworkElement lb = sender as FrameworkElement; MouseOverArdornerInfo ct = GetAttachedAdorner(lb); if (ct == null) return; if (_Adorners.ContainsKey(lb)) return; AdornerLayer layer = AdornerLayer.GetAdornerLayer(lb); ContentPresenter cp = new ContentPresenter(); cp.ContentTemplate = ct.Template; Binding b = new Binding(); b.Path = new PropertyPath("DataContext"); b.Source = lb; b.Mode = BindingMode.OneWay; cp.SetBinding(ContentPresenter.ContentProperty, b); UIElementAdorner uea = new UIElementAdorner(lb) { Child = cp }; //uea.OffsetLeft = ct.VerticalOffset; //uea.OffsetTop = ct.HorizontalOffset; layer.Add(uea); _Adorners.Add(lb, uea); //uea.MouseLeave += new MouseEventHandler(uea_MouseLeave); //uea.MouseDown += lb_MouseDown; //uea.PreviewMouseDown += lb_MouseDown; //uea.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(lb_MouseDown); //uea.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(lb_MouseDown); //uea.MouseLeftButtonUp += new MouseButtonEventHandler(lb_MouseDown); //uea.MouseLeftButtonDown += new MouseButtonEventHandler(lb_MouseDown); //uea.PreviewMouseRightButtonUp += new MouseButtonEventHandler(uea_PreviewMouseRightButtonDown); //uea.PreviewMouseRightButtonDown += new MouseButtonEventHandler(lb_MouseDown); //uea.MouseRightButtonUp += new MouseButtonEventHandler(lb_MouseDown); //uea.MouseRightButtonDown += new MouseButtonEventHandler(lb_MouseDown); }
public PageClone() { content = new ContentPresenter(); content.SetBinding(ContentPresenter.ContentProperty, new Binding("Content") { Source = this }); AddVisualChild(content); }
private void UnmaskHiddenContent() { // LIPEx // if (_contentGrid == null) // { //#if DEBUG // throw new InvalidOperationException("Content grid must be present."); //#endif // return; // } // LPIEx // if (_contentPresenter != null) // { //#if DEBUG // throw new InvalidOperationException("Unmasking cannot happen twice."); //#endif // return; // } //_hasUnmaskedContent = true; var contentBinding = new Binding("Content") { BindsDirectlyToSource = true, Source = this }; var templateBinding = new Binding("ContentTemplate") { BindsDirectlyToSource = true, Source = this }; _contentPresenter = new ContentPresenter { HorizontalAlignment = HorizontalContentAlignment, VerticalAlignment = VerticalContentAlignment, Margin = Padding, IsHitTestVisible = false, }; _contentPresenter.Loaded += OnContentPresenterLoaded; _contentPresenter.Unloaded += OnContentPresenterUnloaded; _contentPresenter.SetBinding(ContentPresenter.ContentTemplateProperty, templateBinding); _contentPresenter.SetBinding(ContentPresenter.ContentProperty, contentBinding); _contentGrid.Children.Add(_contentPresenter); _contentGrid.Visibility = Visibility.Visible; _okToShowAfter = DateTime.Now + MinimumLoadingTime; }
private ContentPresenter GetCurrentContent() { var item = _tabControl.SelectedItem; if (item == null) return null; var tabItem = _tabControl.ItemContainerGenerator.ContainerFromItem(item); if (tabItem == null) return null; var cachedContent = TabContent.GetInternalCachedContent(tabItem); if (cachedContent == null) { cachedContent = new ContentPresenter { DataContext = item, ContentTemplate = TabContent.GetTemplate(_tabControl), ContentTemplateSelector = TabContent.GetTemplateSelector(_tabControl) }; cachedContent.SetBinding(ContentPresenter.ContentProperty, new Binding()); TabContent.SetInternalCachedContent(tabItem, cachedContent); } return cachedContent; }
public TemplatedAdorner(UIElement adornedElement, AdornerLayer adornerLayer) : base(adornedElement) { _adornerLayer = adornerLayer; _contentPresenter = new ContentPresenter(); //绑定内容 var contentBinding = new Binding { Mode = BindingMode.TwoWay, Source = adornedElement, Path = new PropertyPath(MaskAttach.DataContextProperty) }; _contentPresenter.SetBinding(ContentPresenter.ContentProperty, contentBinding); //绑定模板 var contentTemplateBinding = new Binding { Mode = BindingMode.TwoWay, Source = adornedElement, Path = new PropertyPath(MaskAttach.TemplateProperty) }; _contentPresenter.SetBinding(ContentPresenter.ContentTemplateProperty, contentTemplateBinding); //当控件发生大小变化的时候需要强制刷新,不过加这句效率会有影响 //AdornedElement.LayoutUpdated += AdornedElementLayoutUpdated; //加入层中 _adornerLayer.Add(this); //加入可视树是为了防止点击穿过 AddVisualChild(_contentPresenter); }
public void SetFileTabDefinition(FileTabDefinition fileTabDefinition) { this.DataContext = fileTabDefinition; if (fileTabDefinition.IsSeparator) { this.Content = new Separator(); this.IsEnabled = false; } else { this.SetBinding(TabItem.HeaderProperty, new Binding { Source = fileTabDefinition, Path = new PropertyPath(FileTabDefinition.HeaderProperty) }); var presenter = new ContentPresenter(); presenter.SetBinding(ContentPresenter.ContentTemplateProperty, new Binding { Source = fileTabDefinition, Path = new PropertyPath(FileTabDefinition.ContentTemplateProperty) }); this.Content = presenter; this.command = fileTabDefinition.Command as RoutedCommand; if (fileTabDefinition.Command != null) { fileTabDefinition.Command.CanExecuteChanged += OnCommandCanExecuteChanged; this.IsEnabled = this.command == null ? fileTabDefinition.Command.CanExecute(fileTabDefinition.CommandParameter) : this.command.CanExecute(fileTabDefinition.CommandParameter, fileTabDefinition.CommandTarget); } } }
public void DataContextTest_DataContextUpdatesAsync () { var presenter = new ContentPresenter (); presenter.SetBinding (ContentPresenter.DataContextProperty, new Binding ()); Assert.IsInstanceOfType<BindingExpression> (presenter.ReadLocalValue (ContentPresenter.DataContextProperty), "#1"); presenter.Content = new object (); Assert.IsInstanceOfType<BindingExpression> (presenter.ReadLocalValue (ContentPresenter.DataContextProperty), "#2"); TestPanel.Children.Add (presenter); Assert.IsInstanceOfType<BindingExpression> (presenter.ReadLocalValue (ContentPresenter.DataContextProperty), "#3"); Enqueue (() => { Assert.AreSame (presenter.Content, presenter.ReadLocalValue (ContentPresenter.DataContextProperty), "#4"); }); EnqueueTestComplete (); }