I am using a TreeView's in my Visual C++ Solution to represent and alter XML-Content. I use Visual Studio 2010 for this project (.NET/Windows Forms). Some of my TreeNodes have to have Attributes. Unfortunately, TreeNodes in Visual Studio do not seem to have Attributes.
I now wanted to solve this by adding invisible Child-Nodes to TreeNodes which are supposed to have Attributes and saving the Attribute-Value in those invisible Child-Nodes. Again, TreeNodes in Visual Studio do not offer any Property for making them invisible. My Idea now was to simply derive a Custom TreeNode-Class and add a boolean Property whether the TreeNode should be hidden or not. If the property is true I "just" have to overwrite the method of the TreeView which draws the TreeNode when being added (or comes into View or whatever). Unfortunately, that's where I am stuck. Which Function do I have to override to get this done?
BTW: Do I have to add a visibility property? Actually it should suffice to just cast the Node to the Custom-TreeNode (which is the exact same as TreeNode) and then only draw it when it is of Type TreeNode and hide when it is CustomTreeNode. Right?
Thanks for any help!
Related
I have wpf application and I´m creating binding for my control in code behind. The control is added to visual tree and removed from visual tree also in code behind.
It is also necessary to remove this binding in code behind or it will be removed automatically when the control is removed from visual tree?
I have read documentation on MSDN but there is no mention about it.
Thanks
As long as you don't have the binding rooted to somewhere than it will be garbage collected. Xaml is just syntactic sugar over CLR objects.
If you create a binding, and keep a reference to it in some other class, it will not get garbage collected. But if you are just creating the binding, and not keeping a reference to it anywhere, than it will be garbage collected once the Source and Target fall out of Scope.
I'm new to WPF and tyring to uderstand the best way I can modify any given control attributes. What I was trying to achieve is to show tooltip for a cell. Yes a quick google and can see xaml of how to do it. But I want to understand how can one learn to figure out this out using some tool.
I came across Snoop that can show visual tree of a control very easily (CTRL+SHIFT Mouse over). What I'm trying to understand is if one knows of visual tree, how can one change it? For example, let's say I use WPF DataGrid and bind it to a source and display column using DataGridTextColumn.
<DataGridTextColumn Header="First Name" Binding="{Binding FirstName}">
Now let's say I want to show tooltip for each cell. So I fire up Snoop and CTRL+SHIFT mouse over the cell. Snoop shows me that its a DataGridCell that is using Border and withing it ContentPresenter which ends up using TextBlock to show the value. So that means that if I can somehow access that textblock, I can set its tooltip property using binding. Issue is that I don't know how I can access it in xaml.
In other words, knowing a visual tree, how can one access it in xaml for any given control. This will also be very handly for 3rd party controls.
Thanks
Your'e asking quite a bit here, and i'm not sure I completely understand your intention, so I hope I got this right. Your'e asking if you can access the complete visual representation of each control and change it using xaml. The answer to that is yes, but you shouldn't.
I'll get to what I mean in a bit, but first I'd like to clarify some concepts, since i'm not sure you're using them correctly.
XAML:
Xaml is the declarative markup representation of your views and nothing more. Xaml syntax directly corresponds to it's respective classes and their properties. Xaml maps tags to classes and attributes to properties. It's a small distinction, but it's important to think this way. Everything you can do in xaml you can also do in code (although it would often be much more work). Again: xaml refers to markup code only.
<ClassA PropertyA="Value">
<ClassA.PropertyB>
<ClassB />
</ClassA.PropertyB>
Default property value
</ClassA>
Logical tree:
The logical tree is the runtime representation of your xaml code. it consists (mostly) of the controls you set in your xaml files.
Visual tree:
The visual tree is the visual representation the logical tree. It contains much more since it contains the concrete visual representation of everything displayed in your view. Most of the logical tree can't be directly displayed. WPF uses Data and Control Templates together with Styles to determine exactly how each object is supposed to look. In case of data templates that can also mean simple data objects and not only WPF controls.
Now for your question: so can you access the concrete visual representation of each control?
Yes, but you'll have to use control templates to manipulate it's visuals. Also, control templates are usually applied to control types and not specific controls, so you'll have to deal with that as well.
And that's why you shouldn't access it. The xaml representation usually gives you all you need to modify your control, and even if you do use templates you shouldn't change every last piece of it. Templates are used to style a control, so only write enough to show it as you wish. There's no need specify everything.
However you can access the entire visual tree more easily using procedural code, if you use the VisualTreeHelper class (that's how snoop does it, by the way). Using it you can traverse the visual tree and access all it's classes and members. If you really want to access every single visual object you'll do it much more easily with the VisualTreeHelper.
I downloaded a project off codeproject and was messing around with it (I am more an asp.net developer) when I noticed these overlay icons...the lock one on the label is really interesting as when I right click the control is unlocked yet all the properties are grayed out. When I add a new label I do not have that lock icon when I select it. What is this? Also what is the double boxes icon (the other 8 that are highlighted)?
The Winforms designer observes standard .NET accessibility keywords. The Modifiers keyword for a control is what counts here. That sets the access keyword for the member variable. The default for a C# project is private, for a VB.NET project it is Friend. VB.NET is more friendly about it.
That matters when you derive a form from a base form, Project + Add New Item, Windows Forms node, Inherited Form item template. The derived form will have the controls of the base form but they cannot be changed if their Modifiers property is Private. The designer makes it obvious by displaying the lock icon. And by displaying the properties of the control in gray text.
Normally this means that the controls are defined in the base control and so you cannot change them in the derived control (so they are all locked).
I’ve met strange behaviour for WPF design-time in Visual Studio 2010: after an instance of the Popup class was created, and I switched the code tab in Visual Studio to a different file, the Popup still remains on the screen!
I have a piece of code, which allows to reproduce this, but I am not sure if I should paste it here (it's not so short), so maybe I'll just give a link to it: here.
For unknown reasons beyond mere mortals' comprehension, Microsoft has decided this is the default behavior of the Popup class in WPF. You have to implement the "hiding" logic yourself. I suggest handling the Window.LocationChanged, Window.Activated and Window.Deactivated events of the Window containing the Popup and close it yourself.
Edit: To clarify myself, the Window events you need to handle are the events of the window that contains the Popup's PlacementTarget element. Usually when you create a popup, you set it relative to some element contained in an application's Window (similar to how the tooltips work). If this is your case, then my solution is correct, but I forgot to mention this point about the PlacementTarget.
In your code behind; you can simple check this boolean:
DesignerProperties.GetIsInDesignMode(this);
"this" represent the object containing the popup. For example the Window.
If true you can say:
myPopUp.IsOpen = false;
For Store Apps/WinRT:
Windows.ApplicationModel.DesignMode.DesignModeEnabled
I have a real-world problem in which I need to customize the developer experience inside the WPF visual designer and property window of Visual Studio.
I have three properties A B and C:
Each of the three properties must appear in the property window;
Property C must be read-only, and its value must be calculated on the basis of the values of properties A and B, which are read-write; and
If A or B change, the value for C should be updated without having to reload the designer.
How can I achieve this?
Everything related to customizing Visual Studio WPF and Silverlight designers is here.
After checking the documentation and struggling on my own, I have to conclude that what I am trying to do is not possible, since
1) read-only attached dependency properties do not appear in the Visual Studio properties pane to begin with
2) Even if you tried to use DesignModeValueProvider to make an editable property behave like a read-only property, a value that is entered in error will be stored in XAML. This means at runtime you will be setting incorrect values.