×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    3

    Unhappy "System.Windows.Data Error" with a custom ListBoxItemStyle why ?

    Hello !

    In a WP7 application, 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" :

    Code:
    <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"

    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 :

    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);
                }
            }
        }
    Any idea why such an error ? Thank you for any help !

  2. #2
    Nokia Developer Champion
    Join Date
    Sep 2012
    Location
    Morocco
    Posts
    292

    Re: "System.Windows.Data Error" with a custom ListBoxItemStyle why ?

    It's like the ListBoxItemStyle is binding Property1 & Property2 directly from the page DataContext you should separate the DataTemplate from the Style.

    Code:
    <DataTemplate x:Key="ListBoxItemDataTemplate"
              <StackPanel Orientation="Vertical">
                <TextBlock Text="{Binding Property1}"></TextBlock>
                <TextBlock Text="{Binding Property2}"></TextBlock>
              </StackPanel>
    </DataTemplate>
    And in your List
    Code:
    <ListBox ... ... ... ItemsTemplate="{StaticResource ListBoxItemDataTemplate}" />
    Now just put again the ContentControl in the Style

    hope it helps

    Yassine,

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    3

    Re: "System.Windows.Data Error" with a custom ListBoxItemStyle why ?

    Hello !

    Thank you for your answer.

    I understand the error now. But in fact, I wouldn't use the datatemplate because I want to change some properties of my textbox (foreground, brush or layout) on the "Selected/Unselected" visual state of my ListBox. In WP7 data template style triggers doesn't seems to works and in the ItemContainerStyle, I can't access to datatemplate elements by name...

    How can I do that ?

  4. #4
    Nokia Developer Champion
    Join Date
    Sep 2012
    Location
    Morocco
    Posts
    292

    Re: "System.Windows.Data Error" with a custom ListBoxItemStyle why ?

    You could maybe bind the parent style to the Datatemplate

    something like here http://developer.nokia.com/Community...-fill-property

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    3

    Re: "System.Windows.Data Error" with a custom ListBoxItemStyle why ?

    Thanks for your help. I was on holiday, sorry for my late response.

    I don't find a way of doing it with this solution... I should miss something...
    Have you a full sample ? Thanks.

Similar Threads

  1. Replies: 18
    Last Post: 2011-11-08, 11:46
  2. how to change text of "select" and "cancel" button of menu system
    By bigboy0918 in forum Symbian User Interface
    Replies: 1
    Last Post: 2005-11-21, 06:44
  3. Replies: 1
    Last Post: 2005-02-11, 03:53
  4. Replies: 0
    Last Post: 2004-04-02, 03:48
  5. Replies: 2
    Last Post: 2003-08-14, 11:42

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •