繁体   English   中英

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

[英]Visibility type-converter not being called

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

我究竟做错了什么?

谢谢

您的绑定似乎错了:

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

将其更改为:

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

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

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

<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>

如果你没有datacontext就可以完全省略属性的名称,它会起作用,以下对我有所了解。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM