“BindingExpression path error” using ItemsControl and VirtualizingStackPanel

后端 未结 2 1994
时光取名叫无心
时光取名叫无心 2021-02-06 19:03

I\'m using Silverlight on Windows Phone 7.

Is it normal to get loads of \"BindingExpression path error\" debug messages when using a VirtualizingStackPanel? I think it

相关标签:
2条回答
  • 2021-02-06 19:28

    I know this post is pretty old, but a solution without creating the dummy properties is making sure the DataContext of the items can only be of the type you expect.

    I do this with the following converter on the root of the data template:

    public class SpecificTypeConverter : IValueConverter
    {
        public string Type { get; set; }
    
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null || value.GetType().FullName == this.Type)
            {
                return value;
            }
    
            return DependencyProperty.UnsetValue;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    Add a static resource like this:

    <p:PhoneApplicationPage.Resources>
        <converters:SpecificTypeConverter x:Key="MustBeItem" Type="My.Namespace.MyListItemViewModel" /> 
    </p:PhoneApplicationPage.Resources>
    

    And in the data template, use the converter:

    <DataTemplate>
        <StackPanel DataContext="{Binding Converter={StaticResource MustBeItem}}" >
            <TextBlock Tekst="{Binding Path=Name}" />
        </StackPanel>
    </DataTemplate>
    

    Now the Name property of the parent will never be checked.

    0 讨论(0)
  • 2021-02-06 19:39

    I think your assessment of the situation is correct, but it sounds like a bug in the way the VirtualizingStackPanel works. It's not affecting the execution or bindings of your application at the points where you need it, but raising exceptions is a relatively expensive operation and so it would make sense to update the appropriate DataContext accordingly when unlinking the containers.

    I think the best place to raise it as a potential bug would be on the App Hub forums

    0 讨论(0)
提交回复
热议问题