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
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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
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; }
}
}
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}"/>