可见性类型转换器未被调用

时间:2013-02-26 14:46:13

标签: c# wpf xaml type-conversion

我有一个带有以下XAML的测试WPF窗口:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:wpfApplication2="clr-namespace:WpfApplication2"
        xmlns:test="clr-namespace:WpfApplication2.Properties"
        DataContext="{Binding Path=TestClass}"
        Title="MainWindow" Height="350" Width="525" >

    <Window.Resources>
        <wpfApplication2:TestTypeConverter x:Key="TestConverter"/>
    </Window.Resources>

    <Grid>
        <Grid Visibility="{Binding TestProperty, Converter={StaticResource TestConverter}, ConverterParameter='nottest'}">
            <Label Content="Test Label"></Label>
        </Grid>
    </Grid>
</Window>

我有一个测试类型转换器类,如下所示:

using System;
using System.Globalization;
using System.Windows.Data;

namespace WpfApplication2
{
    class TestTypeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var valueToTest = (string) value;
            var parameterToCheck = (string) parameter;

            return valueToTest == parameterToCheck ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
        }

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

TestClass如下:

using System.ComponentModel;
using System.Runtime.CompilerServices;
using WpfApplication2.Annotations;

namespace WpfApplication2
{
    public class TestClass : INotifyPropertyChanged
    {
        public TestClass()
        {
            TestProperty = "test";
        }

        private string _testProperty;
        public string TestProperty
        {
            get { return _testProperty; }
            set
            {
                if (_testProperty == value)
                {
                    return;
                }
                _testProperty = value;
                OnPropertyChanged();
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

可见性属性不受类型转换器的影响,并且甚至没有调用Convert方法本身(我已经放置了一个未被命中的断点)。

我做错了什么?

由于

3 个答案:

答案 0 :(得分:2)

您的绑定似乎有误:

 Visibility="{Binding 'test', Converter={StaticResource TestConverter}, ConverterParameter='nottest'}"

将其更改为:

 Visibility="{Binding test, Converter={StaticResource TestConverter}, ConverterParameter='nottest'}"

如果属性测试不存在,则不会调用转换器

答案 1 :(得分:1)

为转换器参数创建资源(如下面的代码段所示),并使用资源作为转换器参数而不是文字

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:wpfApplication2="clr-namespace:WpfApplication2"
        xmlns:test="clr-namespace:WpfApplication2.Properties"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        DataContext="{Binding Path=TestClass}"
        Title="MainWindow" Height="350" Width="525" >
    <Window.Resources>
        <wpfApplication2:TestTypeConverter x:Key="TestConverter"/>
<sys:String x:Key="converterParam">nottest</sys:String>
    </Window.Resources>

    <Grid>
        <Grid Visibility="{Binding TestProperty, Converter={StaticResource TestConverter}, ConverterParameter={StaticResource converterParam}">
            <Label Content="Test Label"></Label>
        </Grid>
    </Grid>
</Window>

答案 2 :(得分:0)

如果你没有datacontext就可以完全省略属性的名称并且它会起作用,以下内容对我来说很糟糕。

Visibility =“{Binding Converter = {StaticResource myTestConverter}}”

//看看关键字如何跟随关键字Binding关键字,我没有在Binding关键字后指定属性名称。