locked
Custom ListBoxItem Style and "System.Windows.Data Error" why ? RRS feed

  • Question

  • Hello !

    I'am using a listbox with a custom ItemContainerStyle. In it, I replace the "ContentControl" by a custom control. Everything works fine but when I scroll on the list, I get this exception on the output windows :

    "System.Windows.Data Error: BindingExpression path error: 'Property1' property not found on 'PhoneAppForTest.MainPageVM' 'PhoneAppForTest.MainPageVM' (HashCode=107416195). BindingExpression: Path='Property1' DataItem='PhoneAppForTest.MainPageVM' (HashCode=107416195); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Text' (type 'System.String')."

    I don't understand why...

    Here is a link to a project with the problem : http://sdrv.ms/14SXa1M I'am using "Simple Mvvm Toolkit" (the code here is not a "real world" example. It's just to reproduce the bug).

    The "ListBoxItemStyle" :

    <Style x:Key="ListBoxItemStyle1" TargetType="ListBoxItem"> <Setter Property="Background" Value="Transparent"/> <Setter Property="BorderThickness" Value="0"/> <Setter Property="BorderBrush" Value="Transparent"/> <Setter Property="Padding" Value="0"/> <Setter Property="HorizontalContentAlignment" Value="Left"/> <Setter Property="VerticalContentAlignment" Value="Top"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ListBoxItem"> <Border x:Name="LayoutRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}"> <VisualStateManager.VisualStateGroups>

    ...

    </VisualStateManager.VisualStateGroups> <!--<ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>--> <!-- All this code after is normaly a control with properties changing depending on visual state (selected, focus, etc...) using storyboards --> <StackPanel Orientation="Vertical"> <TextBlock Text="{Binding Property1}"></TextBlock> <TextBlock Text="{Binding Property2}"></TextBlock> </StackPanel> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style>

    The "ListBox Code"

    <!--LayoutRoot est la grille racine où tout le contenu de la page est placé-->
    <Grid x:Name="LayoutRoot" Background="Transparent" >
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
    
            <!--TitlePanel contient le nom de l'application et le titre de la page-->
            <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
                <TextBlock x:Name="ApplicationTitle" Text="MON APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
                <TextBlock x:Name="PageTitle" Text="{Binding PageName}" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
            </StackPanel>
    
            <!--ContentPanel - placez tout contenu supplémentaire ici-->
            <ListBox  Margin="12,0,12,0" Grid.Row="1" Name="listBoxTest" VerticalAlignment="Top" ItemsSource="{Binding AllItems}" ItemContainerStyle="{StaticResource ListBoxItemStyle1}" >
                
            </ListBox>
     </Grid>

    The ViewModel Code :

    public class AModel : ModelBase<AModel>
        {
            private int property1;
            public int Property1
            {
                get { return property1; }
                set
                {
                    property1 = value;
                    NotifyPropertyChanged(m => m.Property1);
                }
            }
    
    
            private int property2;
            public int Property2
            {
                get { return property2; }
                set
                {
                    property2 = value;
                    NotifyPropertyChanged(m => m.Property2);
                }
            }
        }
    
    
        public class MainPageVM : ViewModelBase<MainPageVM>
        {
            public MainPageVM()
            {
                this.PageName = "Test Page";
            }
    
    
            public void LoadItems()
            {       
                Task.Factory.StartNew(() =>
                {
                    var res = new ObservableCollection<AModel>();
                    for (int i = 0; i < 1000; i++)
                    {
                        res.Add(new AModel() { Property1 = i, Property2 = 1000 - i });
                    }
    
                    this.AllItems = res;
                });
            }
            
           
            
            private ObservableCollection<AModel> allItems;
            public ObservableCollection<AModel> AllItems
            {
                get { return allItems; }
                set
                {
                    allItems = value;
                    NotifyPropertyChanged(m => m.AllItems);                
                }
            }
    
            private string pageName;
            public string PageName
            {
                get { return pageName; }
                set
                {
                    pageName = value;
                    NotifyPropertyChanged(m => m.PageName);
                }
            }
        }

    Thank you for any help !

    Thursday, August 1, 2013 8:40 PM

All replies

  • You are trying to bind inside a style. It would work if you created a data template for the list item.

    If you want to get it working on a style try setting the relative source to template parent for the binding


    All your questions about windows phone ends here http://howtowindowsphone.com

    Tuesday, August 13, 2013 6:06 AM