I have a custom login usercontrol that has a normal textbox and a passwordbox.
Blend refuses to cooperate with the PasswordBox saying that "DP.UnsetValue" is not valid for PasswordChar property. However the project compiles and runs fine in Blend or in VS2010. The cider designer in VS2010 doesn't seem to mind the error because it actually renders my UserControl for design time configuration.
Normally when I get one of these errors there is an InnerException with a path to the file/resource missing. That's not the case here and I'm not sure how to figure out how to fix it for when this comes up in the future.
I swapped the tags to turn the PasswordBox into a normal TextBox and it seems to be fine with that. However I need the input masking that PasswordBox provides. It's not very practical to comment out that object and finish styling my control in Blend.
Here's my Xaml:
<PasswordBox x:Name="PasswordTextbox" PasswordChar="*" Height="26" VerticalAlignment="Center" Grid.Column="1" Grid.Row="1" Margin="5" RenderTransformOrigin="0.5,0.5" TabIndex="3">
<PasswordBox.RenderTransform>
<TransformGroup>
<ScaleTransform/>
</TransformGroup>
</PasswordBox.RenderTransform>
<PasswordBox.Effect>
<DropShadowEffect />
</PasswordBox.Effect>
<PasswordBox.Triggers>
<EventTrigger RoutedEvent="UIElement.GotFocus">
<BeginStoryboard Storyboard="{StaticResource StoryboardScaleUpFontIncrease}"/>
</EventTrigger>
<EventTrigger RoutedEvent="UIElement.LostFocus">
<BeginStoryboard Storyboard="{DynamicResource StoryboardScaleNormalFontNormal}"/>
</EventTrigger>
</PasswordBox.Triggers>
</PasswordBox>
Does anyone know how to debug this behavior?
I also had the same issue in xaml designer but in a sightly different way. I add the detail here in case it is helpful to someone.
I was using a 3rd party control which has their own theme defined in a dll. I referenced this dll and use 'StaticResource' to reference some resources defined in that dll. Then the xaml designer underlined my xaml code saying something like 'D.Unset is not a valid value for xxx'. But the program compile and run without any problem.
To eliminate these annoying underline, the solution is simple: Change 'StaticResource' to 'DynamicResource'.
Another more complicated way is to use 'ResourceDictionary.MergedDictionaries' to include resources in that dll into your xaml file.
I was finally able to solve this issue by using the techniques described on this blog entry:
http://web.archive.org/web/20090602111317/http://bea.stollnitz.com/blog/?p=52
I advice to recheck (twice or more) your ResourceDictionary. I came to this error after I added to my ResourceDictionary:
<Style TargetType="Label" x:Key="myLabel">
<Setter Property="Foreground" Value="{StaticResource myDefinedColor}"/>
...
</Style>
while {StaticResource myDefinedColor} was defined BELOW it. In my case error was about Foreground that has UnsetValue. So actually it really was an unset value...
I moved my style for Label below defining my color and that was it!
Just wanted to add that this was helpful in solving an issue for getting a "DependencyProperty.UnsetValue is not a valid value for property" error solved, in particular the first part of the blog suggesting that your resource might not be valid (i.e. previously defined and available for use). Turns out in my case it was the classic issue of not defining my resources in the right scope and before they are actually called upon.
My advice: First look to make sure you define the StaticResource in the appropriate Element.Resources section. Make sure that definition is physically located before your actual call to the resource.
Seems an old post. I solved my issue by putting the stuff in order of use in resourceDictionnary merging. In the example below, I called the resource 'Couleur.xaml' later and i had the error message. So I put it in first place and it solved my problem:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/TS;component/Ressources/Couleur.xaml"/>
<ResourceDictionary Source="/TS;component/Ressources/ButtonStyle.xaml"/>
<ResourceDictionary Source="/TS;component/Ressources/DataGridStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
Hope it's gonna help somebody out there!
Related
I believe my question is fairly simple and yet I am having difficulty implementing it successfully. I simply wish to extract the styling of elements in my WPF application because the xaml is rather crowded and xaml is often duplicated.
I therefore wish to place the styling in an external xaml file, in the form of a resource dictionary, then reference that file in the resources section of my code.
I have the following .xaml file:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="PTextBox" TargetType="TextBox" x:Name="PTextBox">
<Setter Property="Foreground" Value="#FFA1C8E7"/>
<Setter Property="BorderBrush" Value="#FFA1C8E7"/>
</Style>
And I reference the dictionary here:
<UserControl.Resources>
<ResourceDictionary x:Key="PegasusStyles">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../../Resources/Styles/PegasusStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
Visual studio has resolved the file location so I know this reference is correct.
The text box the styles are applied to then references the style:
<TextBox Style="{StaticResource PTextBox}"/>
If left as a static resource I get a xaml parse error like so:
An unhandled exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
And if I make the resource dynamic then the styles simply do not get applied at runtime.
I'm not sure if xaml files require certain properties before run time but mine are as follows:
If someone could answer this mystery it would be wonderful. I googled till my fingers bled but none of the answers posted by others have resolved my issues and this seems very rudimentary.
EDIT: Solved. Switching the build action to Page instead of resource has fixed my issue as suggested by Andrew Stephens. This had been hidden by another underlying problem, which is that I had added a boolean to visibility converter (common tool) to my resources. This alone is fine but once I had declared a resource dictionary this converter needed to be brought inside the dictionary as well.
It sounds like a XAML syntax error somewhere, but can also be caused by an unhandled exception in the main window code-behind (if you have any code in here). There are a few ways to debug this cryptic exception here (read the comments for more tips)
Also the Build Action of your .xaml resource file should be "Page" rather than "Resource".
Try building the solution with your newly merged dictionary before you start referencing the external styles in your xaml.
It may seem counter intuitive but it is possible for visual studio to know about a type in another xaml file without the designer being aware which can cause bugs like this.
Koda
I am just starting WPF and it is frustrating the hell out of me. It seems that many properties are null by default (at least those I am working on at the moment) and hence when it compiles and run, nothing happens.
Is there a quick way or a standard workflow procedure to set default values for WPF objects?
For example, I put a Canvas and a Button in XAML view, and then went to code view to add an event handler on the Button to Canvas.Children.Add(new Ellipse()) and then nothing happens. Then I thought maybe I should specify the Width and Height. Still nothing happens. Finally, after much struggling I found the Shape.Stroke property.
Then there is no intuitive Ellipse.X and Ellipse.Y to position the Ellipse. Again, took an hour to find the Canvas.SetLeft().
The final straw is when I try to do Canvas.SetLeft(Random.Next(0, (int)Canvas.Width)); It give a runtime error because Canvas.Width is NULL?!!? Goodness...
Sure, WPF gives a lot of features, but seems like a lot of work coming from a Winforms Graphics.DrawEllipse() .. *sweat*
In WPF if you dont explicitly set the Width/Height in xaml the size will be determined by the Elements layout Container, so to access the Width/Height of an Element like this you use the properties ActualWidth/Actualheight, these return the Rendered size of the Element
Example:
Canvas.SetLeft(Random.Next(0, (int)Canvas.ActualWidth));
If you want to create Default values for a Element you can create a style in xaml for that Element
Example:
<Style TargetType="Ellipse">
<Setter Property="Stroke" Value="Black"/>
</Style>
WPF does have a rough learning curve. One of the tougher things is to dispense somewhat with the techniques you may be used to and embrace the WPF-approach. Xaml is the way to go for defining controls and their properties - Xaml is a language whose only real purpose to do declaration well. In essence, think of the Xaml portion of your code as a glorified constructor.
<Window x:Class="TestWpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="Window"
Title="MainWindow"
Width="640"
Height="480">
<Canvas>
<Ellipse Canvas.Left="50"
Canvas.Top="50"
Width="142"
Height="88"
Fill="Black" />
</Canvas>
</Window>
The declaration above takes advantage of Xaml's nifty syntax for Attached Properties.
You might want to investigate Styles if you find yourself setting a set of common properties on like objects often.
I have a button on a custom control and I'm trying to display an image on it which is defined as a resource in my App.xaml file like so:
<Application.Resources>
<BitmapImage x:Key="PlusSymbol" UriSource="Resources/PlusSymbol.png" />
</Application.Resources>
For some reason I can't use this as a static resource within my custom control's template defined in the Themes\Generic.xaml file, it crashes my application during runtime saying that it cannot find the requested resource. Here's the xaml code I'm using to bind to the resource:
<Button Grid.Row="0" Grid.Column="1" Margin="3">
<Image Source="{StaticResource PlusSymbol}"/>
</Button>
It DOES work during runtime if I define the resource the exact same way but within the Generic.xaml file, however it gives me a pre-compiler warning that it can't find the file since it's now looking for it in Themes/Resources/ rather than just in /Resources/. So I get a warning during design time but it works fine in runtime.
So why can't it find my resource when it's defined in App.xaml? I do this the exact same way in a regular WPF project and it works flawlessly, but in this custom control library it is giving me headaches. Any help is much appreciated!
This should work if you switch your StaticResource to DynamicResource so that the resource will be evaluated dynamically at runtime rather than statically. If you switch to DynamicResource.
<Button Grid.Row="0" Grid.Column="1" Margin="3">
<Image Source="{DynamicResource PlusSymbol}"/>
</Button>
I believe this is because of how theme-based styles and templates are handled, as opposed to standard resources. This answer and this answer speak specifically to Generic.xaml and how it is different from other ResourceDictionaries.
So the way I ended up getting this to work was by defining the BitmapImage in the Generic.xaml file and using a Pack URI to get to the file (here's the MSDN article about Pack URIs, which frankly just confused me). This way it's using a relative path to the file, and specifying the assembly that it's coming from (the file is located at \Resources\PlusSymbol.png in the MyCustomControlLibrary project and has a build action of Resource):
<BitmapImage x:Key="PlusSymbol" UriSource="pack://application:,,,/MyCustomControlLibrary;component/Resources/PlusSymbol.png" />
Then in the control template in Generic.xaml I use the resource like so:
<Button Grid.Row="0" Grid.Column="1" Margin="3" Height="25" Width="25"
<Image Source="{StaticResource PlusSymbol}"/>
</Button>
Note that I got fooled thinking that I could use the shorter version of the Pack URI like so:
<BitmapImage x:Key="PlusSymbol" UriSource="pack://application:,,,/Resources/PlusSymbol.png" />
However this was still causing the program to crash at runtime. I think the fact that this is a custom control library, and that the end consumer of the image is my UI project, makes the longer version which specifies the assembly that actually contains the image necessary.
Hope that helps anyone else who is having similar problems.
Note I don't think this technically answers my original question which specified using app-level resources (aka defined in the custom control library's App.xaml file) from Generic.xaml. Defining the PlusSymbol resource there still crashes the program. However I didn't actually care about doing it in App.xaml, I was just trying to get it to work right at both design time and run time. This solution does the trick for me, is fairly simple, and from what I can tell from my research is the best practice.
In my current project I have a quite large WPF based application with lots of Static and DynamicResources.
Because of many refactorings and changes in the past there are lots of DynamicResources that can not be found during runtime and therefore no value is applied.
What I like to do is run the application and get an output, exception or whatever when a DynamicResource could not be found.
I have tried to build a DefaultTraceListener and a Converter that checks for unused DynamicResources, but to no avail.
Does anyone have a solution for me on how to achieve that?
Example:
<Grid.Resources>
<Style x:Key="myStyle1" TargetType="{x:Type TextBlock}">
<Setter Property="Background" Value="Blue"></Setter>
</Style>
</Grid.Resources>
<StackPanel>
<TextBlock Style="{DynamicResource myStyle1}">DynamicResource exists</TextBlock>
<TextBlock Style="{DynamicResource myStyle3}">DynamicResource does not exist</TextBlock>
</StackPanel>
How can I be informed during runtime that myStyle3 does not exist?
Thanks in advance !
There are many tools that show you witch binding doesn't work..
WPF Inspector is my favorite tool, take a look. WPF Inspector
Snoop utility is one of those. In short - in the top right corner you'll find DropDown list which allows filter visuals, just select Visuals with binding Error. Source: How to locate the source of a binding error?
In Visual Studio, you can enable all exceptions (with binding errors) in the Debug menu, Exceptions, then check everything.
But I don't know if it's exactly what you want, let other people answer this...
I have a loose XAML file...
<Style
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyNamespace"
TargetType="{x:Type local:CustomControl}">
<Setter Property="HoverOpacity" Value="1.0"/>
</Style>
... that I want to load at runtime. When I do I get an exception stating, "Type reference cannot find public type named 'CustomControl'." How can I make the loose XAML aware of my namespace?
I need to use HoverOpacity which is a dependency property of the CustomControl. Here is the code that I am currently using to load the XAML:
var resource = Application.GetResourceStream(new Uri("pack://application:,,,/Assets/HoverStyle.xaml"));
XamlReader.Load(resource.Stream);
BTW, I realize that the XAML is simple and I could just insert the Style in code, but this is a hello world XAML; its going to become a lot more complex, involving animations and such.
P.S. Another solution would be a way of either attaching a XAML file to a custom control derived from Panel (one that doesn't crash Visual Studio 2008) or a way of easily attaching triggers, data-triggers, entry-actions, and exit actions to custom controls.
Gosh darn it, I figured it out. I needed to specify the assembly name with the namespace; like so:
<Style
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyNamespace;assembly=MyAssembly"
TargetType="{x:Type local:CustomControl}">
<Setter Property="HoverOpacity" Value="1.0"/>
</Style>
I'll give answer credit to anyone who could answer my "P.S." question within the next two days. This whole situation seems a little wet, so I'd be really interested in alternatives.
Thanks :)