WPF绑定转换器无法正常工作

时间:2010-09-23 16:45:02

标签: wpf binding

我有一个具有DefaultText属性的自定义转换器。如果字符串为null或为空,我的所有转换器都会返回DefaultText。我似乎无法让它工作。这就是我所拥有的。这是转换器类。

public class DisplayValueConverter : DependencyObject, IValueConverter
{
    public static readonly DependencyProperty DefaultTextProperty = DependencyProperty.Register( "DefaultText",
                                                                                                 typeof ( string ),
                                                                                                 typeof ( DisplayValueConverter ) );

    public string DefaultText
    {
        get { return ( string ) GetValue( DefaultTextProperty ); }
        set { SetValue( DefaultTextProperty, value ); }
    }

    public object Convert( object value, Type targetType, object parameter, CultureInfo culture )
    {

        string empty = ( parameter != null ) ? parameter as string : DefaultText;
        return ( value != null && !string.IsNullOrEmpty( value.ToString().Trim() ) ) ? value.ToString() : empty;
    }

    public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture )
    {
        return null;
    }
}

这是我的xaml投入使用。

        <TextBox Grid.Column="1"
                 Grid.Row="3"
                 VerticalAlignment="Center"
                 Margin="0,0,10,0" >
            <TextBox.Text>
                <Binding Path="DataSource.Payee"
                         Mode="TwoWay"
                         NotifyOnSourceUpdated="True"
                         NotifyOnTargetUpdated="True"
                         NotifyOnValidationError="True"
                         ValidatesOnDataErrors="True"
                         UpdateSourceTrigger="PropertyChanged">
                    <Binding.Converter>
                        <k:DisplayValueConverter DefaultText="{Binding ElementName=This, Path=Test, Mode=TwoWay}" />
                    </Binding.Converter>
                    <Binding.ValidationRules>
                        <vr:RequiredField Label="Payee" />
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>

我已经验证了DataContext有一个对象,并且Path有效。所以我不确定我做错了什么。

3 个答案:

答案 0 :(得分:1)

我认为问题在于您使用Binding.ElementName。因为您的值转换器实际上不是可视树或逻辑树的一部分,所以绑定引擎无法知道它需要遍历哪个树以便找到具有匹配的ElementName的元素。

在这种情况下,您最好的选择是从代码后面专门设置绑定的Source属性,或者创建一个自定义标记扩展,为您抓取正确的对象。

答案 1 :(得分:0)

只是为答案#1 添加注释 - 即使转换器位于可视化或逻辑树中,您也不应该假设在DefaultText执行之前设置Convert(...)转换器中的{1}}。

答案 2 :(得分:0)

在我的情况下,我忘记了WPF要求必须使用getter和setter明确声明对代码隐藏值的绑定为公共属性。

所以失败了:

public partial class MyPage: Page
{
    public bool ShowLabel;  //No get/set !

    // constructor etc...
}

<Page x:Name="pageName">

    <Label Content="Hello" 
           Visibility="{Binding 
                           Path=ShowLabel, 
                           ElementName=pageName, 
                           Converter={StaticResource VisibilityConverter}}"/>

</Page>

这根本没有转换。断点没有达到。没事。

通过向我正在使用的属性中添加吸气剂和设置剂,页面可以识别值,并且绑定/转换器可以按要求工作。

public partial class MyPage: Page
{
    public bool ShowLabel { get; set; }
}
相关问题