执行时间中的绑定错误

时间:2016-02-16 17:56:24

标签: .net wpf xaml data-binding

我正在尝试消除所有运行时绑定错误以加快我的应用程序,但我有一些错误让我遇到麻烦而且我无法解决。 我的主要问题在于显示3D内容的用户控件。 我用一段代码展示它们:

<Viewport3D xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
        x:Name="viewport" ClipToBounds="False" IsHitTestVisible="False">
<Viewport3D.Camera>
    <PerspectiveCamera FieldOfView="45" NearPlaneDistance="0.125" FarPlaneDistance="Infinity" Position="-176.298724337988,214.871512878159,289.862943629443" LookDirection="0.469846310392954,-0.342020143325669,-0.813797681349374" UpDirection="0.171010071662834,0.939692620785908,-0.296198132726024"/>
</Viewport3D.Camera>
    <ModelVisual3D>
    <ModelVisual3D.Content>                    
        <Model3DGroup>
            <Model3DGroup.Transform>
                <Transform3DGroup>
                    <ScaleTransform3D ScaleZ="{Binding CurrentScale}" ScaleY="1" ScaleX="1"/>
                    <RotateTransform3D>
                        <RotateTransform3D.Rotation>
                            <AxisAngleRotation3D Axis="0,-1,0" Angle="{Binding CurrentAngle}"/>
                        </RotateTransform3D.Rotation>
                    </RotateTransform3D>
                    <MatrixTransform3D Matrix="1,1,-1,0,1,1,-3,0,0,-1,3,0,0,48,32,1"/>
                </Transform3DGroup>
            </Model3DGroup.Transform>
            <GeometryModel3D x:Name="Box02">
                <GeometryModel3D.Geometry>
                    <MeshGeometry3D Positions="1 1 1 "/>
                </GeometryModel3D.Geometry>
                <GeometryModel3D.Material>
                    <DiffuseMaterial Brush="{Binding CurrentColor}"/>
                </GeometryModel3D.Material>
            </GeometryModel3D>
            <Model3DGroup>
                <DirectionalLight Direction="{Binding CurrentLight}" Color="#FFFFFF" x:Name="light"/>
            </Model3DGroup>
        </Model3DGroup>
    </ModelVisual3D.Content>                               
</ModelVisual3D>
</Viewport3D>

我的绑定如下,我有MSDN中的类型(附加在绑定) https://msdn.microsoft.com/en-us/library/system.windows.media.media3d.directionallight.direction(v=vs.110).aspx 键入:System.Windows.Media.Media3D.Vector3D

public System.Windows.Media.Media3D.Vector3D CurrentLight
{
    get { return this.currentLight; }
    set
    {
        this.currentLight = value;
        this.OnPropertyChanged("CurrentLight");
    }
}
protected System.Windows.Media.Media3D.Vector3D currentLight;

https://msdn.microsoft.com/en-us/library/system.windows.media.media3d.scaletransform3d.scalez(v=vs.110).aspx 键入:System.Double

public double CurrentScale
{
    get { return this.currentScale; }
    set
    {
        this.currentScale = value;
        this.OnPropertyChanged("CurrentScale");
    }
}
protected double currentScale;

https://msdn.microsoft.com/en-us/library/system.windows.media.media3d.axisanglerotation3d.angle(v=vs.110).aspx 键入:System.Double

public double CurrentAngle
{
    get { return this.currentAngle; }
    set
    {
        this.currentAngle = value;
        this.OnPropertyChanged("CurrentAngle");
    }
}
protected double currentAngle;

https://msdn.microsoft.com/en-us/library/system.windows.media.media3d.diffusematerial.brush(v=vs.110).aspx 键入:System.Windows.Media.Brush

public System.Windows.Media.Brush CurrentColor
{
    get { return this.currentColor; }
    set
    {
        this.currentColor = value;
        this.OnPropertyChanged("CurrentColor");
    }
}
protected System.Windows.Media.Brush currentColor;

虽然绑定正常工作但运行时会显示以下消息:

System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=CurrentScale; DataItem=null; target element is 'ScaleTransform3D' (HashCode=27183531); target property is 'ScaleZ' (type 'Double')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=CurrentAngle; DataItem=null; target element is 'AxisAngleRotation3D' (HashCode=2594575); target property is 'Angle' (type 'Double')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=CurrentColor; DataItem=null; target element is 'DiffuseMaterial' (HashCode=7565331); target property is 'Brush' (type 'Brush')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=CurrentLight; DataItem=null; target element is 'DirectionalLight' (HashCode=4095240); target property is 'Direction' (type 'Vector3D')

在Stackoverflow中搜索后,¿我找到一个关于使用“后备值”的解决方案?这是最好的解决方案吗? Getting many Binding "Information" in WPF output window

我在另一个线程堆栈溢出中看到他们建议使用以下语句(How to eliminate Binding “Information” in WPF output window - cannot retrieve value using the binding):

System.Diagnostics.PresentationTraceSources.DataBindingSource.Switch.Level = System.Diagnostics.SourceLevels.Critical;

无论如何,为什么我会收到这些错误?我理解我使用的类型是正确的,因为我已经在MSDN提供的链接中检查了它们,这是正确的吗?

由于

2 个答案:

答案 0 :(得分:0)

我不知道您是否指定了数据绑定的数据上下文。您是否已将以下代码添加到构造函数中?如果没有,那应该是你的问题。数据绑定需要知道从哪里获取属性。

viewport.DataContext = this;

我发现this文章在这方面非常有用。特别是这一段:

  

绑定包含源和目标;绑定框架负责处理来自源和(可选)目标的更改通知,保持两者同步。我们的FieldUserControl中的绑定有一个Path值,它指定目标,但源是什么?   如果在XAML中创建绑定但未指定源(可能是最常见的用例),则源将设置为已指定绑定的控件的DataContext。 DataContext在可视树下继承,从每个控件的父级到子级。 DataContext通常设置为视图模型或业务/模型对象,在我们的情况下,顶级控件MainPage将其DataContext设置为ModelObject的实例。   因此,FieldUserControl的DataContext及其所有子元素也是ModelObject。这就是我们的价值绑定失败的原因。 Value是FieldUserControl的属性,而不是我们的模型对象。

答案 1 :(得分:0)

我假设您指定的属性位于ViewModel中。尝试将ViewModel作为资源添加到您的用户控件,并将其设置为数据上下文

xmlns:viewModels="clr-namespace:My.App.ViewModels.Test"

<UserControl.Resources>
    <viewModels:YourViewModelClass x:Key="myViewModel"/>
</UserControl.Resources>

<Grid DataContext="{Binding Source={StaticResource myViewModel}}">

     <!-- Your XAML Code Here -->

</Grid>