我有一个带有以下 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 方法本身(我放置了一个没有被命中的断点)。
我究竟做错了什么?
谢谢