DataTemplate和WrapPanel可见性

时间:2013-12-20 12:05:27

标签: c# wpf datatemplate wrappanel

我收到了这个错误:

  

在'System.Windows.Markup.StaticResourceHolder'上提供一个值   异常。

使用xaml代码:

 <WrapPanel Orientation="Horizontal" Grid.Row="0" >
    <WrapPanel.Visibility>
      <Binding Path="setVisible" Converter="{StaticResource BooleanToVisibilityConverter}" ConverterParameter="{Binding setVisible}"/>
    </WrapPanel.Visibility>
                                         //textblocks goes here
  </WrapPanel>

和班级:

public class dataTemplate_xItem
    {
        (...)
        public bool setVisible { get; set; }
        public sealed class BooleanToVisibilityConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                var flag = false;
                if (value is bool)
                {
                    flag = (bool)value;
                }
                else if (value is bool?)
                {
                    var nullable = (bool?)value;
                    flag = nullable.GetValueOrDefault();
                }
                if (parameter != null)
                {
                    if (bool.Parse((string)parameter))
                    {
                        flag = !flag;
                    }
                }
                if (flag)
                {
                    return Visibility.Visible;
                }
                else
                {
                    return Visibility.Collapsed;
                }
            }

            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                var back = ((value is Visibility) && (((Visibility)value) == Visibility.Visible));
                if (parameter != null)
                {
                    if ((bool)parameter)
                    {
                        back = !back;
                    }
                }
                return back;
            }
        }
    }

在我将项目添加到ListView之前,请检查

if(myValue != 0)
    newItem.setVisible = true;
else
    newItem.setVisible = false;

知道出了什么问题吗? :)

1 个答案:

答案 0 :(得分:1)

冰山是正确的。 ConverterParameter不是DependencyProperty,因此无法绑定。查看您的xaml,您不需要ConverterParameter。您也不需要绑定表达式的扩展标记。你xaml可以简单地

<UserControl.Resources>
    <BooleanToVisibilityConverter x:Key="Boolean2Visibility" />
</UserControl.Resources>

<WrapPanel Orientation="Horizontal" Grid.Row="0" Visibility="{Binding Path=setVisible, Converter={StaticResource Boolean2Visibility}}" />

此代码假定您在UserControl中