visual-tree

WPF: In an attached property, how to wait until visual tree loaded properly?

非 Y 不嫁゛ 提交于 2019-12-22 09:26:04
问题 I have an Attached Property in a WPF app. The code below is inside the OnLoad event, but it doesn't work unless I add a hacky 500 millisecond delay in. Is there some way to avoid this delay, and detect when the visual tree has been loaded? private static void FrameworkElement_Loaded(object sender, RoutedEventArgs e) { // ... snip... Window window = GetParentWindow(dependencyObject); // Without this delay, changing properties does nothing. Task task = Task.Run( async () => { { // Without this

Move a UserControl from a ContentControl to another one programmatically

你离开我真会死。 提交于 2019-12-21 01:17:20
问题 In a WPF app I want to move a UserControl from a ContentControl to another one in code: myContentControl2.Content = myUserControl; in this case I get an error: Specified element is already the logical child of another element. Disconnect it first. In a ControlControl class description I can see a RemoveVisualChild method, but when I try to use it in code I get an Unknown method error myContentControl1.RemoveVisualChild(myUserControl);//here I get an "Unknown method" error Where am I wrong?

Datagridcolumn: Binding Visibility and Width via BindingProxy show different behavior

醉酒当歌 提交于 2019-12-20 07:30:00
问题 As the columns of a data grid aren't in the visual tree of datagrid I'm m using this approach of Binding Proxy to bind Visibility of a DataGridTextColumn . https://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/ For some reason I would like to understand the same approach does work fine for Visibility but not for column's Width property. Could someone explain this different behavior to me? Code sample c# class BindingProxy : Freezable { #region

wpf force to build visual tree

青春壹個敷衍的年華 提交于 2019-12-17 20:38:15
问题 I have ItemsControl with Grid as ItemsPanelTemplate <ItemsControl ItemsSource="{Binding CellCollection}" Name="CellGrid"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <Grid Name="grid" /> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> </ItemsControl> I create some UserControl with this ItemControl inside in code-behind, and then i need to create RowDefinitions and ColumnDefinitons. I use this method to get "grid": private TChildItem FindVisualChild<TChildItem>(DependencyObject obj) where

How to force DataGrid to rebuild VisualTree for its columns

余生长醉 提交于 2019-12-12 03:43:13
问题 I have WPF form with DataGrid. New columns can be added to the datagrid manually by user via button. This is the code to add new column: private void ColumnAdornerAddButton_MouseDown(object sender, MouseButtonEventArgs e) { DataGridTextAdornerColumn column = new DataGridTextAdornerColumn(); column.Header = "New column"; column.HeaderStyle = (Style)FindResource("columnHeader"); column.AdornerTemplate = (DataTemplate)FindResource("columnAdorner"); Binding binding = new Binding("Data"); binding

Why does binding fail when binding a child element to another element when the parent succeeds?

与世无争的帅哥 提交于 2019-12-06 13:17:46
Say I have two classes that can reference a third UI object (in this example a button). In addition, the parent class can contain an element of the child class. If they both are bound to the same control, the same way, the child will fail but the parent succeed. Is this a bug in WPF? The parent : class MyFrameworkElement : FrameworkElement { // A depenedency property that will contain a child element sub-element private static readonly DependencyProperty ChildElementProperty = DependencyProperty.Register("ChildElement", typeof(MyChildElement), typeof(MyFrameworkElement), new PropertyMetadata()

“FindName” doesn't work if an element added in code

有些话、适合烂在心里 提交于 2019-12-06 07:38:04
问题 In a WPF app, if a ContentControl is declared in XAML, <Grid Name="MyGrid"> <ContentControl Name="MyContentControl" /> </Grid> then I can easily reference it in code using FindName : ContentControl cc = FindName("MyContentControl") as ContentControl; cc.Content = ... But if I add the ContentControl in code instead: ContentControl contentcntr = new ContentControl(); contentcntr.Name = "MyContentControl"; this.MyGrid.Children.Add(contentcntr); The FindName doesn't find it. What's wrong with it

Why AdornerLayers are always the top most layers? Is there a way to change it?

喜欢而已 提交于 2019-12-06 07:28:18
问题 Why is that the adorner layer is always rendered as the top most layer in an application (under AdornerDecorator - refer screenshot)? Is there a way to change the layer/level on to which the adorners can be drawn? In the following screenshot, AdornerLayer is added to AdornerDecorator and the Adorners (MyAdorners) are added to this AdornerLayer. But the AdornerLayer is retrieved like this, AdornerLayer layer1 = AdornerLayer.GetAdornerLayer(button1); layer1.Add(new MyAdorner(button1)); 回答1: To

“FindName” doesn't work if an element added in code

拈花ヽ惹草 提交于 2019-12-04 14:58:32
In a WPF app, if a ContentControl is declared in XAML, <Grid Name="MyGrid"> <ContentControl Name="MyContentControl" /> </Grid> then I can easily reference it in code using FindName : ContentControl cc = FindName("MyContentControl") as ContentControl; cc.Content = ... But if I add the ContentControl in code instead: ContentControl contentcntr = new ContentControl(); contentcntr.Name = "MyContentControl"; this.MyGrid.Children.Add(contentcntr); The FindName doesn't find it. What's wrong with it in the second case? What's the difference? The XAML parser automatically registers the names in a

Why AdornerLayers are always the top most layers? Is there a way to change it?

▼魔方 西西 提交于 2019-12-04 13:20:17
Why is that the adorner layer is always rendered as the top most layer in an application (under AdornerDecorator - refer screenshot)? Is there a way to change the layer/level on to which the adorners can be drawn? In the following screenshot, AdornerLayer is added to AdornerDecorator and the Adorners (MyAdorners) are added to this AdornerLayer. But the AdornerLayer is retrieved like this, AdornerLayer layer1 = AdornerLayer.GetAdornerLayer(button1); layer1.Add(new MyAdorner(button1)); To answer my second question, Is there a way to change the layer/level on to which the adorners can be drawn? I