1

我有一个带有以下 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;
        }
    }
}

测试类如下:

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 方法本身(我放置了一个没有被命中的断点)。

我究竟做错了什么?

谢谢

4

3 回答 3

2

您的绑定似乎错误:

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

将其更改为:

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

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

于 2013-02-26T14:49:53.897 回答
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>
于 2013-02-26T15:54:31.453 回答
0

如果您没有数据上下文,只需完全省略属性的名称,它就会起作用,以下内容对我来说很重要。

可见性="{绑定转换器={StaticResource myTestConverter}}"

// 看看Converter关键字后面是怎么跟Binding关键字的,我没有在Binding关键字后面指定属性名。

于 2013-09-13T13:54:31.737 回答