Information when DynamicResource could not be found during runtime? - wpf

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...

Related

Windows Phone 8.1, How to bind style to view?

I wrote a style for button. Unfortunately, I encountered a problem: font size of the button depends on a value, which I can compute only in the view (specifically, that's DisplayInformation stuff).
Ideally, I would do the following:
<Style x:key="MyStyle" BasedOn="{StaticResource SomeStyle}" TargetType="Button">
<Setter Property="FontSize" Value="{Binding ElementName=rootControl, Path=SomeProperty" />
</Style>
Then, I would simply provide necessary properties in view class. But that simply doesn't work (does nothing, no messages, no errors).
How can I solve this problem?
Assigning a binding to a setter value is not supported in the Windows Runtime. (It might be supported in WPF and Silverlight 5, however).
If you google around, there are workarounds, but they're kind of hacky. See this, which uses attached properties and a binding helper class.

Visual 2010 WPF Designer: Styles sometimes shown/not shown?

Ok, I'm pretty new to WPF programming. Now I always run across the following issue so maybe anyone could lighten me up:
In My Window I have a datagrid that I want to style (column should be green). So I create a ressource:
<Window.Resources>
<Style TargetType= "{x:Type DataGridCell}" x:Key="GreenColumns">
<Setter Property="Background" Value="SeaGreen" />
</Style>
</Window.Resources>
Now I have a Column like that:
<DataGridTextColumn Binding="{Binding N}" Header="N" IsReadOnly="True" CellStyle="{StaticResource GreenColumns}">
It is working, no problem at all!
Now all I want is to apply this resourced style via the Designer in VS 2010.
To do that I click on my DatagridTextColumn. Next I click in "CellStyle" (Property view on the left of course) . In this dropdown there is always a style called "Standard" and sometimes there are self-defined styles too, but not very often. In this case I think I should be able to select my style "GreenColumns". Instead it shows Key: GreenColumns preceeded with a yellow exclamation mark symbol.
I don't get it. What's wrong here? What has to be done in order to show a self-defined style in this dropdown?
I've given up on the Visual Studio XAML designer, even in 2010. There seems to be just too many legitimate techniques that it just cannot handle. I'd say learn XAML code and/or Expression Blend.

How to make loose Xaml content aware of custom controls

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 :)

How to debug DependencyProperty.Unsetvalue errors when the InnerException is null

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!

Reclaiming the space from the DescriptionViewer part of the DataFields on a Silverlight Toolkit DataForm

The DescriptionViewer part of the DataField is used to display the Description property of the System.ComponentModel.DisplayAttribute as a ToolTip in the generated form. I don't want to use this capability and although I can make sure the UI element is not visible by using a style to set either the DescriptionViewerVisibility to Collapsed or by setting the DescriptionViewerStyle to be null as shown below, there is still space reserved in the DataField layout for this element.
<Style x:Key="DataFieldStyle1" TargetType="dataFormToolkit:DataField">
<Setter Property="DescriptionViewerVisibility" Value="Collapsed"/>
<Setter Property="DescriptionViewerStyle" Value="{x:Null}" />
</Style>
This space is as waste in my scenario and I want to get rid of it. I would expect this layout to be exposed by the DataField.Template property but when I use Blend to edit a copy of the default template the layout is not there.
I'm using the System.Windows.Controls.Data.DataForm.Toolkit, Version=2.0.5.0 from the October 2009 release of the Silverlight Toolkit within a WCF RIA Services Beta Business Application Silverlight 3 project. I'm using Visual Studio 2008 SP1. I know there is a November 2009 release but I can't see any mention of this changing in the release notes.
An alternative solution is to use DataForm Label and a control to display your field.
Instead of using a DataField like this and eventually having space for DescriptionViewer
<dataControls:DataField>
<TextBox Text="{Binding FirstName, Mode=TwoWay}" />
</dataControls:DataField>
You can use this code, and you will not have the DescriptionViewer
<dataInput:Label Target="{Binding ElementName=tbFirstName}" />
<TextBox x:Name="tbFirstName" Text="{Binding FirstName, Mode=TwoWay}" />
With this solution, you will loose the generated layout that come with the DataForm but you can do it easily with a simple Grid
Using Reflector I can see that the DataField.OnApplyTemplate method calls a private method called GenerateUI, which uses conventional code to create a Grid with a Column for the DescriptionViewer, and I can't see a way to prevent this, without doing some very low level .NET clr kind of hack which would be inappropriate. Am I missing something here?
I'm starting to come to the conclusion that you either need to stick very close to the default behaviour of these Silverlight Toolkit controls if you want to benefit from the supposed productivity gains. Anything more that pretty trivial customisation seems to be an incomplete story at the moment.

Resources