binding to abstraction collection of abstraction class

essamce 621 Reputation points
2020-06-28T05:34:00.69+00:00

hi , i have :

public class Tangent
{
 public IPoint2d StartPoint {get; set;}
...
}

...

public IList<ITangent> Tangents = new ObservableCollection<ITangent>();
Tangents.Add(new Tangent(new Point2d()));

...


    <ListBox 
                   ItemsSource=
                    "{Binding Tangents,
                        UpdateSourceTrigger=PropertyChanged}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock 
                                Text="{Binding StartPoint,
                                UpdateSourceTrigger=PropertyChanged}"/>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

the binding doesn't work.
any help would be appreciated,
i'm using wpfCore3.1 vs2019

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,801 questions
0 comments No comments
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,326 Reputation points
    2020-06-29T19:13:48.733+00:00

    Hi, the binding doesn't work because Tangents is not property. Try following demo:

    <Window x:Class="WpfApp1.Window04"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApp04"
            mc:Ignorable="d"
            Title="Window04" Height="450" Width="800">
      <Window.DataContext>
        <local:ViewModel/>
      </Window.DataContext>
      <Grid>
        <ListBox ItemsSource="{Binding Tangents}">
          <ListBox.ItemTemplate>
            <DataTemplate>
              <TextBlock Text="{Binding StartPoint.Description, UpdateSourceTrigger=PropertyChanged}"/>
            </DataTemplate>
          </ListBox.ItemTemplate>
        </ListBox>
      </Grid>
    </Window>
    

    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Windows;
    
    namespace WpfApp04
    {
      public class ViewModel
      {
        public ViewModel()
        {
          Tangents.Add(new Tangent(new Point2d()));
        }
        public IList<ITangent> Tangents { get; set; } = new ObservableCollection<ITangent>();
      }
    
      public class Tangent : ITangent
      {
        public Tangent(Point2d pt) => StartPoint = pt;
        public IPoint2d StartPoint { get; set; }
      }
    
      public interface ITangent
      {
        IPoint2d StartPoint { get; set; }
      }
    
      public class Point2d : IPoint2d
      {
        public string Description { get; set; } = "new Description";
      }
    
      public interface IPoint2d
      {
        string Description { get; set; }
      }
    }
    
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Ken Tucker 5,856 Reputation points
    2020-06-28T10:18:04.587+00:00

    Is Tangents a property? You can only bind to properties. The code posted makes it look like a field

        public IList<ITangent> Tangents = new ObservableCollection<ITangent>();
    

    StartPoint is a class Point2d I think you need to bind to one of the properties in the Point2d class

                             <TextBlock 
                                 Text="{Binding StartPoint.MyProperty,
                                 UpdateSourceTrigger=PropertyChanged}"/>
    
    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.