如果绑定失败,如何指定默认值?

时间:2016-03-29 23:07:50

标签: c# wpf xaml

我有一个DrawingBrush如下: -

1) binary tree tests adds values to the binary tree:

    AssertionError: expected { Object (root) } to equal { Object (root) }
    + expected - actual

我在样式中使用了上面的画笔如下: -

<DrawingBrush x:Key="MY_ICON" Viewbox="0,0,39.125,39.125" ViewboxUnits="Absolute">
    <DrawingBrush.Drawing>
        <GeometryDrawing Brush="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=uiEntityViews:BaseView}, Path=Color}" Geometry="F1M19.5625,0.999954C29.8144,0.999954 38.125,9.31053 38.125,19.5625 38.125,29.8142 29.8143,38.1249 19.5625,38.1249 9.31073,38.1249 1,29.8142 1,19.5625 1,9.31053 9.31064,0.999954 19.5625,0.999954z">
            <GeometryDrawing.Pen>
                <Pen DashCap="Square" EndLineCap="Flat" LineJoin="Round" MiterLimit="10" StartLineCap="Flat" Thickness="2">
                    <Pen.Brush>
                        <LinearGradientBrush EndPoint="0.849422,0.849423" StartPoint="0.150577,0.150578">
                            <GradientStop Color="#FF657783" Offset="0"/>
                            <GradientStop Color="White" Offset="0.146"/>
                            <GradientStop Color="#FF2C4758" Offset="1"/>
                        </LinearGradientBrush>
                    </Pen.Brush>
                    <Pen.DashStyle>
                        <DashStyle/>
                    </Pen.DashStyle>
                </Pen>
            </GeometryDrawing.Pen>
        </GeometryDrawing>
    </DrawingBrush.Drawing>
</DrawingBrush>

现在,我收到此错误 - <Style x:Key="NODE_ICON" TargetType="Rectangle"> <Setter Property="Fill"> <Setter.Value> <DrawingBrush TileMode="None"> <DrawingBrush.Drawing> <DrawingGroup> <GeometryDrawing Brush="{StaticResource MY_ICON}"> <GeometryDrawing.Geometry> <RectangleGeometry Rect="0,0,1,1" /> </GeometryDrawing.Geometry> </GeometryDrawing> <DrawingGroup> <DrawingGroup.Transform> <TranslateTransform X="0.2" Y="0.2" /> </DrawingGroup.Transform> <GeometryDrawing Brush="{StaticResource NODE_ICON}"> <GeometryDrawing.Geometry> <RectangleGeometry Rect="0,0,1,1" /> </GeometryDrawing.Geometry> </GeometryDrawing> </DrawingGroup> </DrawingGroup> </DrawingBrush.Drawing> </DrawingBrush> </Setter.Value> </Setter>

似乎绑定无法正常工作。有没有办法指定颜色的默认值,如果绑定失败,可以说System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='Views.UIEntityViews.BaseView', AncestorLevel='1''. BindingExpression:Path=Color; DataItem=null; target element is 'GeometryDrawing' (HashCode=3377278); target property is 'Brush' (type 'Brush')

2 个答案:

答案 0 :(得分:3)

使用FallbackValue .....<GeometryDrawing Brush="{StaticResource NODE_ICON, FallbackValue=Gray}">

答案 1 :(得分:1)

正如@Carmine建议使用FallbackValue,我能够让它进行以下更改: -

<GeometryDrawing Brush="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=uiEntityViews:CanvasLinkConnectionPointBaseView}, Path=MeSiteColor, FallbackValue=Gray}" Geometry="F1M19.5625,0.999954C29.8144,0.999954 38.125,9.31053 38.125,19.5625 38.125,29.8142 29.8143,38.1249 19.5625,38.1249 9.31073,38.1249 1,29.8142 1,19.5625 1,9.31053 9.31064,0.999954 19.5625,0.999954z">