XAML中的可见性转换器绑定

时间:2012-04-03 10:40:09

标签: wpf xaml binding

我有以下转换器:

public class MyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return Visibility.Hidden;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
...

然后我在XAML中使用它如下:

<Resources>
    <conv:MyConverter x:Key="MyValToVisibilityConverter" />
</Resources>

...

<CheckBox x:Name="MyCheckBox" Content="Should not be visible" 
                Visibility="{Binding ElementName=Visibility, Converter={StaticResource MyValToVisibilityConverter}}" 
...

这会编译并运行,并始终将复选框显示为可见。

1 个答案:

答案 0 :(得分:3)

您正在为绑定设置错误的参数。

现在,绑定目标是一个名为Visibility的元素,很可能是一个错误:

Visibility="{Binding ElementName=Visibility, Converter={...}}"

应该只是

Visibility="{Binding Converter={...}}"