wpf:当前绑定项为null时覆盖标签内容

时间:2010-08-05 06:55:49

标签: wpf

只要绑定值为null,我想设置Label(或TextBox)默认字符串。这适用于除Content以外的任何其他属性,例如:

        <Label Content="{Binding Source={StaticResource pumpCurvesViewSource}, Path=/Label}">
            <Label.ContentStringFormat>Details for pump curve: {0}</Label.ContentStringFormat>

            <Label.Style>
                <Style TargetType="Label" BasedOn="{StaticResource header}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Source={StaticResource pumpCurvesViewSource}, Path=/}" Value="{x:Null}">
                            <Setter Property="Background" Value="Red"></Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Label.Style>
        </Label>

只要CurrentItem中没有pumpCurvesViewSource,上述内容就会将标签的背景更改为红色,但我真正想做的是将背景触发更改为:

<Setter Property="Content" Value="No pump curve selected"></Setter>

但这不起作用(我猜这是因为Content已经绑定,我的设置会覆盖绑定)。

有谁知道如何做到这一点?

3 个答案:

答案 0 :(得分:1)

你试过TargetNullValue吗?

<Label Content="{Binding Source={StaticResource pumpCurvesViewSource}, Path=/Label,  TargetNullValue='No pump curve selected'}">

答案 1 :(得分:0)

如果找到一种方法来完成同样的事情,但代码多于我喜欢的代码:

            <Label Content="{Binding Source={StaticResource pumpCurvesViewSource}, Path=/Label}">
                <Label.ContentStringFormat>Details for pump curve: {0}</Label.ContentStringFormat>
                <Label.Style>
                    <Style TargetType="Label" BasedOn="{StaticResource header}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Source={StaticResource pumpCurvesViewSource}, Path=/, Mode=OneWay, Converter={StaticResource isNullConverter}}" Value="True">
                                <Setter Property="Visibility" Value="Collapsed"></Setter>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Label.Style>
            </Label>

            <Label Content="No pump curve selected">
                <Label.Style>
                    <Style TargetType="Label" BasedOn="{StaticResource header}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Source={StaticResource pumpCurvesViewSource}, Path=/, Mode=OneWay, Converter={StaticResource isNullConverter}}" Value="False">
                                <Setter Property="Visibility" Value="Collapsed"></Setter>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Label.Style>
            </Label>

答案 2 :(得分:0)

最短的答案是:

<Label Content="{Binding Source={StaticResource pumpCurvesViewSource, FallbackValue=Your Default String}, Path=/Label}">