Torus class TubeDiameter Property binding fails in WPF UserControl
See original GitHub issueI created a user control containing a TorusVisual3D object inside HelixViewport3D. Everything works. But when I bind to the TubeDimaterProperty in XAML or in code my program crashes. I am not very advanced in WPF. I was not able to pinpoint the exact issue. Since other properties work well and binding in code also fails for the TubeDimaterProperty, I am inclined to think the problem is with the TubeDiameter property. I looked into the source code, as I indicated I am not very advanced in WPF.
<UserControl x:Class="Torus2020A.TorusUserControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:hx="clr-namespace:HelixToolkit.Wpf;assembly=HelixToolkit.Wpf" xmlns:local="clr-namespace:Torus2020A" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> <Grid> <DockPanel> <hx:HelixViewport3D x:Name="UCTorusView" ModelUpDirection="0,1,0" ZoomExtentsWhenLoaded="True" ShowCoordinateSystem="True" PanGesture="LeftClick"> <hx:TorusVisual3D x:Name="UCTorus" TorusDiameter="{Binding Path=UCTorusDiameter, RelativeSource={RelativeSource AncestorType=UserControl, AncestorLevel=1}}" TubeDiameter="{Binding Path=UCTubeDiameter, RelativeSource={RelativeSource AncestorType=UserControl, AncestorLevel=1}}" ThetaDiv="{Binding Path=UCThetaDiv, RelativeSource={RelativeSource AncestorType=UserControl, AncestorLevel=1}}" PhiDiv="{Binding Path=UCPhiDiv, RelativeSource={RelativeSource AncestorType=UserControl, AncestorLevel=1}}" /> </hx:HelixViewport3D> </DockPanel> </Grid> </UserControl>
This is the failing XAML (removing TubeDiameter or assigning a value to it works):
This is the binding in Code:
// var b = new Binding("UCTubeDiameter"); // b.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(UserControl), 1); // BindingOperations.SetBinding(UCTorus, TorusVisual3D.TubeDiameterProperty, b);
I tried Visual Studio 2017 with Framework Version 4.6 and 2019 with 4.7. The results were the same. Thank you for a wonderful contribution. Please let me know if you need the whole solution.
Issue Analytics
- State:
- Created 3 years ago
- Comments:16 (11 by maintainers)
Top GitHub Comments
Attached please find the offending code (Solution) created with Visual Studio 2017. If you change the TubeDiameter entry in the TorusUserControl.XAML to TubeDiameter=“1”, it works. Thank you very much for torus20.zip your attention and time.
@holance I have made another test for this issue. I just have removed the exception from the AddTorus method.
And now the original code from I. Konuk (without defining a default value for the tube diameter) is running.
These are my changes in the MeshBuilder class (Line 3024):
I saw 2 lines in the debug output. So the “tubeDiamater == 0.0” case came two times, but after this everything was OK.
I would suggest to remove the exception and the debug code and just do nothing in case of tube diameter == 0. If the diameter is zero, nothing will be drawn.