Visual Studio provides a helpful tool window, which allows investigating values of WPF elements in the runtime:
Is there a way to name instances of eg. styles so that I can identify, which specific style got applied to a property of some element?
I'd expect something like System.Windows.Style (name: Abcd)
I tried adding x:Name and x:Uid, but neither worked in the expected way.
Related
Is there a way to see which UserControl/Window/etc is being instantiated when these errors are happening?
A typical error looks like this:
System.Windows.Data Error: 40 : BindingExpression path error: 'BackgroundColor' property not found on 'object' ''MapContainerViewModel' (HashCode=25350572)'. BindingExpression:Path=BackgroundColor; DataItem='MapContainerViewModel' (HashCode=25350572); target element is 'SolidColorBrush' (HashCode=35109313); target property is 'Color' (type 'Color')
Problem? Where is it happening? In which XAML file? In which class?
Doesn't it seem bizarre that you get a list of symptoms, but not the patient's name?
In a small project I would probably know where to look. I'm currently refactoring an MVVM project with hundreds of Windows/UserControls, many of which have similar looking Binding Paths.
So I have to do an "Entire Solution" search for the property names and come up with a list of candidates (UserControls/Windows) that may have caused the Binding error when they were instantiated.
Another option would be to have Visual Studio break and show me the XAML as soon as a System.Windows.Data error occurs. Following tutorials like this one, I can get the code to break--but it doesn't indicate any XAML or class name. The stack trace shows nothing--as the controls are being automatically created via MEF.
Thanks for any help.
Chad.
UPDATED:
Live Visual Tree doesn't work in this case, because the UserControls/Windows in question are sitting in an MEF container and aren't attached.
Any visual approach won't work because the views/datacontexts (view models, in this case) were instantiated via MEF (DI pattern) and are waiting to be added to the visual tree.
The debugger is not going to help you very much with XAML. This is because XAML is declarative while the debugger is designed for imperative code. For WPF you need to rely on a different tool set, specifically the Live Visual Tree and the Live Property Explorer in VS2017.
Here is a simple WPF app with a few textbox controls.
One of the text box controls is not working. How do I find it? First open the Live visual tree. Click the enable selection button in the WPF tool bar (the second button). Then select the control which is not working. The live visual tree will select the control.
Now select the TextBox parent control in the Live Visual Tree (FirstValueField). Now open the Live Property Explorer.
Note the yellow box around the Text property. This is where the error is. Click to the right of the property field and select "Go To Source".
So here is where the error is. You can see where I changed the code to introduce the error.
This is the general approach to debugging WPF, XAML or any declarative code: (1) give up on the debugger...it will simply get in the way; (2) learn how to use the fit-for-purpose tools to find errors; (3) if that doesn't work, use print statements.
In my experience the WPF tools are excellent for finding common errors. The more complex your WPF becomes however, the more you will need to embed code to diagnose and identify the problem.
I would like to set the default font of the form components from Microsoft Sans Serif to MS Outlook
I can change the font every time I put a new control on the form but its time consuming. I didn't find any help or options for it in the Visual Studio 2012.
How can I change the default font for any added control?
Many Controls you add to a Form, default to some of the Form's properties. That includes the Font of the Form as well as its BackColor. This comes handy if you want to use, say Consolas,10 for all Controls..
Here is MSDN on these 'ambient properties'..:
An ambient property is a property on a control that, if not set, is
retrieved from the parent control. If the control does not have a
parent and the property is not set, the control tries to find the
value of the ambient property through the Site property. If the
control is not sited, the site does not support ambient properties, or
the property is not set on the AmbientProperties object, the Control
uses its own default values. Some objects derived from the Control
class might set the property even if you do not. For example, the Form
class always sets the ForeColor and BackColor properties.
TextBoxes and some other Controls don't get the Backcolor, though.
Note: Changing the Form's font will change those 'inherited' Fonts of all Controls on the Form, including TextBoxes, Lists etc. Those properties you have set directly will not change, though.
So: If you want to use varying Fonts, get the Form's Font right first and try to avoid an uncontrolled mix of default and set values! (You can check which you have set in the From.Designer.cs file..)
I have the same question which bothers me very much, and I can not find the solution for months. Today I finally find a possible solution using my limited concepts on c#.
Back to the topic, just add the 2 lines below in the file "form1.designer.cs", which is in the installation directory of visual studio. My visual studio 2010 have the directory like this :
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ProjectTemplatesCache\CSharp\Windows\1033\WindowsApplication.zip
using System.Drawing; ///this line on top of all
this.Font = new Font("Arial", 16); ///this line in the InitializeComponent()
There are some side effects because some properties rely on the font size, such as the form size will grow because of the Form's AutoScaleMode, default size of button/textbox would be not suitable as you know... But it is not a big issue. A nice programmer could solve this kind of issue by himself.
In this manner you could change anything, such as button/lable font, color... All depend on your imagination.
This is my first post. I hope it helps some guys like me.
The easiest way i found is find and replace feature.
Just double click an item lets say a command button then inside the code hit Ctrl +F to find "font". after you find which default or current font is in use, now broaden the Find to Find and Replace and now replace with your desired font.
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.
In Expression Blend 4 the Simple Styles use the following XML namespace:
http://schemas.microsoft.com/expression/interactivedesigner/2006
It frequently uses an attribute from this namespace called IsControlPart which is a boolean. I've googled extensively and can't find any actual documentation on this attribute.
I think I figured it out.
When WPF creates the default templates for its controls, it names some of the elements with the prefix "PART". For example, with a Slider control, it has two parts, PART_Track and PART_SelectionRange. This in itself has nothing to do with Expression Blend.
These parts necessary for the basic functionality of the control. Most simple controls, like a Button, do not have any parts, so they can be fully customized without having any restrictions.
More complex controls rely on parts being present in order for the code-behind to work properly. It doesn't care what the control looks like, or where they are in the visual tree, so long as they exist and are of the right type.
In Expression Blend, there is a "Parts" window which is by default a tab alongside the "Project" and "Assets" tabs. When you are editing a template for a Control, if there are any parts defined for that control it lists them in this Parts window. It shows a check mark if that part exists in the current template, and if not you can double click on it and Blend will create the element for you.
I am still not sure exactly how the d:IsControlPart attribute works. I tried making my own custom Control and creating a element with the IsControlPart tag, and then creating a template for that control to see if it would list the part. It didn't. So this is still not 100% answered.
somehow I am going in circles here. Please forgive me if the answer to this question is obvious.
I want to react to changed properties in the ViewModel in the View. When the properties (bool) change the View should start an animation (BeginStoryBoard).
Actually in my application there are 4 of these properties each with its own name in the VM and two desired animations each (hide/show) for the respective 4 container view elements.
When setting these Triggers (tried DataTrigger/Trigger/EventTrigger) directly in the respective container elements (all of them of custom type SizerControl derived from ContentConrol) wpf gave me errors suggesting using a style. When setting a style directly in the container elements I got: "The tag 'Style.Triggers' does not exist in XML namespace ..."
But when using a style in a global ResourceDictionary I am at a loss how to bind to the 4 containersto get the "input" value for the trigger.
Do you have a suggestion how to solve this from an architecture pov? Maybe I am already down too far the wrong path to see the obvious solution.
Thanks a bunch,
Hinnerk
You can do this with a DataTrigger and a StoryBoard.
MSDN has a full sample describing the process, with code, documented in How to: Trigger an Animation When Data Changes.