Question

I want to display collection of double objects.

Following code works fine:

namespace WpfApplication4
{
public partial class MainWindow : Window
{
    public ObservableCollection<double> collection1 { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        collection1=new ObservableCollection<double>();

        for (double d = 0.0; d < 1.0; d += 0.1)
        {
            nm.wartosci.Add(d);
        }

        DataContext=this;

    }
}

XAML file:

<Window x:Class="WpfApplication4.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>

    <ListView ItemsSource="{Binding wartosci}" >
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel />
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
    </ListView>
</Grid>
</Window>

But what if I want to display objects in different way. When I add DataTemplate to ListView, so it looks as below:

<ListView ItemsSource="{Binding wartosci}" >
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel />
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
        <ListView.ItemTemplate>
            <DataTemplate>
                <TextBox Text="{Binding}"/>
            </DataTemplate>
        </ListView.ItemTemplate>
 </ListView>

Visual studio returns an exception:

XamlParseException was unhandled Two-way binding requires Path or XPath.

I suppose I should write something into {Binding}, but what is it?

No correct solution

OTHER TIPS

Try this:

<ListView.ItemTemplate>
    <DataTemplate>
        <TextBox Text="{Binding Path=.}"/>
    </DataTemplate>
</ListView.ItemTemplate>

Edit: I had a little more thought about this and it actually cannot work that way. What your are trying to do is actually the same thing as trying to assign a new value to a parameter. That is why TwoWay binding cannot work.

One way to do it would be to have a wrapper around your double. Therefore the wrapper object is not changed but you can access and change the double value inside the wrapper.

WrapperClass:

public class WrapClass
{
    public double Value { get; set; }
}

...

public ObservableCollection<WrapClass> Collection1 { get; set; }

Your XAML:

<ListView.ItemTemplate>
    <DataTemplate>
        <TextBox Text="{Binding Value }"/>
    </DataTemplate>
</ListView.ItemTemplate>

I hope this helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top