Feb 10, 2018

Resolving harmless binding errors in WPF

While developing WPF applications, you will notice a lot of binding errors being displayed in output window; like this
System.Windows.Data Error: 4 : Cannot find source for binding with reference 
'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGrid', 
AncestorLevel='1''. BindingExpression:Path=CellsPanelHorizontalOffset; DataItem=null; 
target element is 'Button' (Name=''); target property is 'Width' (type 'Double')

System.Windows.Data Error: 4 : Cannot find source for binding with reference 
'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', 
AncestorLevel='1''. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; 
target element is 'ComboBoxItem' (Name=''); target property is 
'HorizontalContentAlignment' (type 'HorizontalAlignment')

System.Windows.Data Error: 4 : Cannot find source for binding with reference 
'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', 
AncestorLevel='1''. BindingExpression:Path=VerticalContentAlignment; DataItem=null; 
target element is 'ComboBoxItem' (Name=''); target property is 
'VerticalContentAlignment' (type 'VerticalAlignment')
I also faced this problem and tried a lot of things to get to the root cause of the problem. It was very frustrating as AncestorLevel is not used anywhere in code! and I was not able to find the place in code which is responsible for these errors.
Even after searching the various forums and articles there was no solution for this problem; but this was a very common issue and cause of this problem as mentioned on various forums:
This is a "known" issue, and happens to all controls that contain dynamically created lists (all item controls i.e. ComboBox, menu, ListBox etc.).
ControlTemplate of items in these controls (specifically MenuItem, ComboBoxItem etc.)
try to find the nearest ItemsControl and bind to the VerticalAlignment and HorizonalAlignment properties and raises this error on not finding the source.
Microsoft guys mention here and here that “This error has already been handled internally, so you can just leave it alone.” But, still I wanted some sort of solution so as not to have so many irritating error messages in my output window;
Solution 1:
So, I tried one of the workaround provided for this problem i.e. to explicitly set the properties which cause problems like this -
<ComboBox
    Name="control">
    <ComboBox.ItemContainerStyle>
        <Style
            TargetType="ComboBoxItem">
            <Setter
                Property="HorizontalContentAlignment"
                Value="Left" />
            <Setter
                Property="VerticalContentAlignment"
                Value="Center" />
        </Style>
    </ComboBox.ItemContainerStyle>
</ComboBox>
This works but it’s very hard task to set these properties across the whole solution for each problematic control(i.e. ListBox, Menu, ContextMenus etc.).
Have a look at this SO question for some other workarounds for this problem - ListBox with Grid as ItemsPanelTemplate produces weird binding errors
Solution 2:
Another workaround is to suppress these errors (actually, it seems more appropriate to call them warnings) by setting the data binding source switch level as critical in constructor of the class or a top level window -
#if DEBUG 
System.Diagnostics.PresentationTraceSources.DataBindingSource.Switch.Level = System.Diagnostics.SourceLevels.Critical;
#endif
I find this one line workaround more appropriate as it works for all such controls (like Menus, ListBox etc.) and that too across the whole project  -

REFERENCE : https://weblogs.asp.net/akjoshi/resolving-un-harmful-binding-errors-in-wpf

Unity Volumetric Light Error on Mac OSX

When I get build from Unity for Mac OS X and run the game on Mac OSX, I get

ERROR:

Initialize engine version: 2017.3.0f3 (a9f86dcd79df)
GfxDevice: creating device client; threaded=1
2018-02-07 12:42:57.410 btm[28692:5744936] Color LCD preferred device: Intel HD Graphics 5000 (high power)
2018-02-07 12:42:57.410 btm[28692:5744936] Metal devices available: 1
2018-02-07 12:42:57.410 btm[28692:5744936] 0: Intel HD Graphics 5000 (high power)
2018-02-07 12:42:57.411 btm[28692:5744936] Forcing user selected device: Intel HD Graphics 5000 (high power)
Initializing Metal device caps: Intel HD Graphics 5000
Begin MonoManager ReloadAssembly
- Completed reload, in  0.137 seconds
WARNING: Shader Unsupported: 'Sandbox/VolumetricLight' - Pass '' has no vertex shader
ERROR: Shader Shader is not supported on this GPU (none of subshaders/fallbacks are suitable)WARNING: Shader Unsupported: 'Sandbox/VolumetricLight' - Setting to default shader.
WARNING: Shader Unsupported: 'Hidden/BilateralBlur' - Pass '' has no vertex shader
ERROR: Shader Shader is not supported on this GPU (none of subshaders/fallbacks are suitable)WARNING: Shader Unsupported: 'Hidden/BilateralBlur' - Setting to default shader.
WARNING: Shader Unsupported: 'Hidden/VideoDecodeOSX' - Pass 'FLIP_RGBARECT_TO_RGBA' has no vertex shader
WARNING: Shader Unsupported: 'Hidden/VideoDecodeOSX' - Setting to default shader.
WARNING: Shader Unsupported: 'Hidden/BlitToDepth' - Pass '' has no vertex shader
WARNING: Shader Unsupported: 'Hidden/BlitToDepth' - Setting to default shader.
WARNING: Shader Unsupported: 'Hidden/BlitToDepth_MSAA' - Pass '' has no vertex shader
WARNING: Shader Unsupported: 'Hidden/BlitToDepth_MSAA' - Setting to default shader.
Metal RecreateSurface[0x10d626e60]: surface size 1440x900
Setting up 2 worker threads for Enlighten.
  Thread -> id: 7000014b6000 -> priority: 1
  Thread -> id: 700001539000 -> priority: 1
UnloadTime: 4.903137 ms

Trying to access pass 4, but material 'Sandbox/VolumetricLight' subshader (0) has only 1 valid passes.
UnityEngine.Material:SetPass(Int32)
VolumetricLight:SetupDirectionalLight(VolumetricLightRenderer, Matrix4x4) (at C:\Users

Invalid pass number (5) for Graphics.Blit (Material "Hidden/BilateralBlur" with 1 passes)
UnityEngine.Graphics:Internal_BlitMaterial(Texture, RenderTexture, Material, Int32, Boolean)
UnityEngine.Graphics:Blit(Texture, RenderTexture, Material, Int32) (at /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/GraphicsBindings.gen.cs:2343)
VolumetricLightRenderer:OnRenderImage(RenderTexture, RenderTexture) (at /Users/Shared/Unity/eatallthe/Assets/Volumetric/Scripts/VolumetricLightRenderer.cs:343)

FIX:  

1- Edit > Project Settings > Player


2 - From Other Settings > Rendering, Uncheck "Auto Graphics API for Mac"

4- Re-Order as OpenGLCore appears above Metal


Visual Studio Keyboard Shortcuts

Playing with keyboard shortcuts is very interesting and reduce the headache of using the mouse again and again while programming with visu...