2013-12-20 62 views
0

我得到這個錯誤:的DataTemplate和WrapPanel知名度

Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.

使用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; 

任何想法出了什麼問題? :)

+0

我看到至少有一件事是錯誤的,勢必'ConverterParameter'。有關詳細信息,請參閱[此答案](http://stackoverflow.com/a/15309844/1021555)。 – icebat

回答

1

icebat是正確的。 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}}" /> 

此代碼假定您是在用戶控件