ListView items do not display - wpf

I have a rather simple ListView with GridView style. Cells are binded to some properties of an ObservableCollection elements. I have no idea why thaey are not displaying. I were thinking about implementing INotifyPropertyChanged in a class of element contained by collection, but once an element is constructed and added to a collection it is never changed so it should be working just fine.
C#:
public ObservableCollection<SceneElement> SceneObjects { get; set; }
...
SceneObjects.Add(new Camera());
SceneObjects.Add(new Cuboid(20, 30, 40));
Camera and Cuboid both inherit from SceneObject (SceneObject <- Figure <- Cuboid and SceneObject <- Camera)
XAML:
<ListView Grid.Column="2" Margin="5"
ItemsSource="{Binding }"
DataContext="{Binding SceneObjects}"
ScrollViewer.CanContentScroll="True">
<ListView.Resources>
<GridView x:Key="StyleOne">
<GridViewColumn Header="Type" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="1" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Position" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="X: " />
<TextBlock Text="{Binding Position.X, RelativeSource={RelativeSource Mode=FindAncestor}}" />
<TextBlock Text=" Y: " />
<TextBlock Text="{Binding Position.Y}" />
<TextBlock Text=" Z: " />
<TextBlock Text="{Binding Position.Z}" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.Resources>
<ListView.Style>
<Style TargetType="ListView">
<Setter Property="View" Value="{StaticResource StyleOne}" />
</Style>
</ListView.Style>
</ListView>
Position property is defined inside an abstract SceneElement class.
Position's X, Y and Z are properties as well.
EDIT
This has solved the issue:
private ObservableCollection<SceneElement> sceneObjects;
public ObservableCollection<SceneElement> SceneObjects
{
get
{
return sceneObjects;
}
set
{
sceneObjects = value;
NotifyPropertyChanged("SceneObjects");
}
}
Can anyone explain why this was necessary? I've used ObservableCollection severeal time so far and it always worked without notifying.

Reason why your application wasn't displaying the information about the collection is because the UI wasn't aware of any changes.
Using ObservableCollection doesn't guarantee the results will be displayed if said collection is not initialized before it is used. As it is a plain property on the ViewModel it needs to notify if said object, in this case ObservableCollection has been changed, but not in the sense items inside of the collection.
This is due to the fact of NOT raising the PropertyChanged event for SceneObjects, which is crucial for WPF apps using Bidning.

at the point where the wpf system attempts to create the binding, your observablecollection was null, as xamimax above said.
instantiate your observablecollection in your constructor, and later you can add the items and they will appear.
for example, the following works because i first instantiated the observable collection and then added items 5 seconds later.
public MainWindow()
{
InitializeComponent();
this.SceneObjects = new ObservableCollection<MyClass>();
Task.Delay(5000).ContinueWith((old) =>
{
this.Dispatcher.Invoke(() =>
{
this.sceneObjects.Add(new MyClass("name"));
this.sceneObjects.Add(new MyClass("name"));
});
});
this.DataContext = this;
}
and here is an example that doesnt work, because i instantiate the observable collection 5 secs after the wpf system tried to bind to it:
public MainWindow()
{
InitializeComponent();
Task.Delay(5000).ContinueWith((old) => { this.SceneObjects = new ObservableCollection<MyClass> { new MyClass("name"), new MyClass("name") }; });
this.DataContext = this;
}
and as you already realized, you can throw propchanged to make wpf system display those items, but i do think you should simply instantiate the observablecollection earlier so that you dont need to throw propchanged later.

Related

Control template for ListViewItem - how to access property of item

Solved:
I was an idiot and trusted the editors information that the DataContext is wrong. The solution is simply
<TextBlock Text="{Binding A}" />
I added a TextBlock beneath each displayed Item of a ListView. For this I used a ControlTemplate with the target type set to "ListViewItem". I put the GridViewRowPresenter and the TextBlock into a StackPanel.
<ListView ItemsSource="{Binding Items}">
<ListView.Resources>
<ControlTemplate x:Key="CustomListViewItemTemplate" TargetType='{x:Type ListViewItem}'>
<StackPanel>
<GridViewRowPresenter Content="{TemplateBinding Content}"
Columns="{TemplateBinding GridView.ColumnCollection}"/>
<TextBlock Text="{Binding }" /> <!-- here I fail -->
</StackPanel>
</ControlTemplate>
</ListView.Resources>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Template" Value="{StaticResource CustomListViewItemTemplate}"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
...
</GridView>
</ListView.View>
</ListView>
The ItemsSource of the ListView is a ObservableCollection Items = new ObservableCollection<Item>();
with Item as
public class Item
{
public string A { get; set; }
public string B { get; set; }
public string C { get; set; }
}
I can access the ListViewItem in the ControlTemplate, but not the Item itself. Is there a possibility bind the TextBlock in the ControlTemplate to e.g. the Property "A" of each instance of Item?
With <ListView ItemsSource="{Binding Items}"> you specify which collection (In this case Items) provides data for the ListView. Within the template, if you assign the value "{Binding}", you effectively assign an Item from your collection you specified as ItemsSource.
To assign a binding to a property of your Item, you already found the solution
<TextBox Text="{Binding A}"/>
Additionally to your current solution,
<TextBlock Text="{Binding Path=A, Mode=TwoWay}"/>
above syntax allows you to navigate to nested members, and set additional binding related parameters like Mode, UpdateSourceTrigger, Converter etc.

Array of string not binding to the listview

I'm trying to bind an array to list view. It's not binding and showing blank. I used Model class to place array.
View Model:
public class RunningLateOptions
{
public string[] runningLateOptions = new[] { "30 Mins", "1 Hour", "1:30 Hour", "2 Hours" };
public string[] runningLateOption
{
get{ return runningLateOptions; }
}
}
XAML code:
<ListView x:Name="RunningLateOptions" ItemsSource="{Binding RunningLateOptions}" ItemSelected="runningLateSelectedItem">
<ListView.ItemTemplate>
<DataTemplate>
<Label x:Name="listItems" Text="{Binding runningLateOption}" HorizontalTextAlignment="Center"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
I'm not able to understand what's wrong with this. Help me.
You need to modify the xaml as follows:-
<ListView x:Name="RunningLateOptions" ItemsSource="{Binding runningLateOption}" ItemSelected="runningLateSelectedItem">
<ListView.ItemTemplate>
<DataTemplate>
<Label x:Name="listItems" Text="{Binding .}" HorizontalTextAlignment="Center"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Let me know if you face more difficulties.
The type ItemsSource of ListView or CollectionView should be a list which implement the Interface IEnumerable .
So we normally set it as an ObservableCollection<T> or List<T> .
In your case , you could set the ItemsSource of the ListView like
public ObservableCollection<string> RunningLateOptions {get; set;}
ObservableCollection has implemented the Interface INotifyPropertyChanged. So you don't need to implement it any more .
For more details about ListView you could refer https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/listview/data-and-databinding
<ListView x:Name="RunningLateOptions" ItemsSource="{Binding RunningLateOptions}" ItemSelected="runningLateSelectedItem">
<ListView.ItemTemplate>
<DataTemplate>
<StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<Label x:Name="listItems" Text="{Binding runningLateOption}" HorizontalTextAlignment="Center"/>
</StackLayout>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
I gave the array of strings in the CS file and bind to listview by using itemsource.
I didn't use any View model here.
CS code:
string[] runningLateOptions = { "30 Mins", "1 Hour", "1:30 Hour", "2 Hours" };
RunningLateOptions.ItemsSource = runningLateOptions;
XAML code:
<ListView x:Name="RunningLateOptions" ItemsSource="{Binding Items}" ItemSelected="runningLateSelectedItem">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label BackgroundColor="Transparent" x:Name="listItems" Text="{Binding}" TextColor="Black" HorizontalOptions="Center"></Label>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
TextCell not providing the HorizontalTextAlignment attribute. That's why I used view cell for the label.
Thanks for helping me.
Click here to see the output

Checkbox with DataGrid WPF

I am trying to create a DataGrid in WPF 4.0 using MVVM...
Features required -
Muti - Select rows using a checkbox (single click)
A select all checkbox to check all the checkboxes in the datagrid
Something like this -
It has been 2 days and i am not able to figure out how to solve the problem effectively..
A working example is what I need now ASAP..
I'll highly appreciate if someone has a working solution to share with me...
N please don't tell me to google this thing because none of the things worked out for me...
UPDATE -
I am using AutoGeneration of Columns
I don't want to add "IsSelected" or any of such property in my MODEL..
I am just facing 2 problems -
Firstly, "Select all" Feature i.e. checking all checkboxes on the checkbox click of the one present in the column header...(I am able to select and unselect the datagrid but not able to tick/untick the checkboxes)
Secondly, Multiple selection on mouse click without holding Ctrl Key..
When you're working with MVVM, you have to be aware of what is considered data and what is strictly UI.
Is your SelectedItems going to be part of your data, or only your UI?
If it's part of your data, you really should have an IsSelected property on your data model, even if that means extending the data class to include an IsSelected property, or creating a wrapper class that only contains bool IsSelected and object MyDataItem. The first option is probably preferred, since you could keep AutoGenerateColumns="True", and it makes the column bindings simpler.
Then you would just bind your DataGridRow.SelectedItem to the IsSelected property of the data item:
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="IsSelected" Value="{Binding IsSelected}" />
</Style>
But if your SelectedItems is only for the UI, or if you are breaking the MVVM pattern for some reason in this instance, than you can create the unbound CheckBox and use some code behind to ensure the CheckBox is correctly synchronized to the SelectedItem.
I did a quick sample app, and here is what my code looked like:
First off, I just added the unbound CheckBox column to the column list using a DataGridTemplateColumn. This will get added before the AutoGenerateColumns list of columns.
<DataGrid x:Name="TestDataGrid" ItemsSource="{Binding Test}"
SelectionMode="Extended" CanUserAddRows="False"
PreviewMouseLeftButtonDown="TestDataGrid_PreviewMouseLeftButtonDown_1">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox x:Name="TestCheckBox"
PreviewMouseLeftButtonDown="CheckBox_PreviewMouseLeftButtonDown" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Second, I added a PreviewMouseDown event to the CheckBox to make it set the IsSelected property of the row.
private void CheckBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var chk = (CheckBox)sender;
var row = VisualTreeHelpers.FindAncestor<DataGridRow>(chk);
var newValue = !chk.IsChecked.GetValueOrDefault();
row.IsSelected = newValue;
chk.IsChecked = newValue;
// Mark event as handled so that the default
// DataGridPreviewMouseDown doesn't handle the event
e.Handled = true;
}
It needs to navigate the VisualTree to find the DataGridRow associated with the clicked CheckBox to select it, and to make life easier I am using some custom VisualTreeHelpers that I have on my blog to find the DataGridRow. You can use the same code, or you can create your own method for searching the VisualTree.
And last of all, if the user clicks on anywhere other than the CheckBox, we want to disable the default DataGrid selection event. This ensures that the IsSelected value will only change when you click on the CheckBox.
There are multiple ways of doing this that will disable the selection at different levels, but to make life simple I just disabled the DataGrid.PreviewMouseLeftButtonDown event if the user didn't click on the CheckBox.
private void TestDataGrid_PreviewMouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
{
var chk = VisualTreeHelpers.FindAncestor<CheckBox>((DependencyObject)e.OriginalSource, "TestCheckBox");
if (chk == null)
e.Handled = true;
}
I using my custom VisualTreeHelpers again to navigate the visual tree and find out if the CheckBox was clicked on, and cancelling the event if the user clicked on anywhere other than the CheckBox.
As for your 2nd request of adding a CheckBox to SelectAll or UnselectAll items, this would once again be dependent on if your selection is part of the UI or the data.
If it's part of the UI, simply add a CheckBox to the DataGridTemplateColumn.HeaderTemplate, and when it's clicked, loop through the DataGrid.Rows, find the CheckBox in the first column, and check or uncheck it.
If it's part of the data you could still do the same thing (only set the bound value in the DataGrid.Items instead of the CheckBox.IsChecked from the DataGrid.Rows), or you could do as Adolfo Perez suggested, and bind it to a property on the ViewModel.
For an MVVM Solution you could try this:
<StackPanel>
<DataGrid ItemsSource="{Binding Path=TestItems}" AutoGenerateColumns="False" Name="MyDataGrid"
CanUserAddRows="False">
<DataGrid.Columns>
<DataGridCheckBoxColumn Binding="{Binding IsSelected}" Width="50" >
<DataGridCheckBoxColumn.HeaderTemplate>
<DataTemplate x:Name="dtAllChkBx">
<CheckBox Name="cbxAll" Content="All" IsChecked="{Binding Path=DataContext.AllSelected,RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
</DataTemplate>
</DataGridCheckBoxColumn.HeaderTemplate>
</DataGridCheckBoxColumn>
<DataGridTemplateColumn Header="Name" Width="SizeToCells" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
In your ViewModel:
private void PopulateTestItems()
{
TestItems = new ObservableCollection<TestItem>();
for (int i = 0; i < 5; i++)
{
TestItem ti = new TestItem();
ti.Name = "TestItem" + i;
ti.IsSelected = true;
TestItems.Add(ti);
}
}
private bool _AllSelected;
public bool AllSelected
{
get { return _AllSelected; }
set
{
_AllSelected = value;
TestItems.ToList().ForEach(x => x.IsSelected = value);
NotifyPropertyChanged(m => m.AllSelected);
}
}
private ObservableCollection<TestItem> _TestItems;
public ObservableCollection<TestItem> TestItems
{
get { return _TestItems; }
set
{
_TestItems = value;
NotifyPropertyChanged(m => m.TestItems);
}
}
And finally the sample Model class:
public class TestItem : ModelBase<TestItem>
{
private string _Name;
public string Name
{
get { return _Name; }
set
{
_Name = value;
NotifyPropertyChanged(m => m.Name);
}
}
private bool _IsSelected;
public bool IsSelected
{
get { return _IsSelected; }
set
{
_IsSelected = value;
NotifyPropertyChanged(m => m.IsSelected);
}
}
}
Most of the code above should be self-explanatory but if you have any question let me know
Your view can be something like
<DataGrid Name="SomeDataGrid" Grid.Row="0" ItemsSource="{Binding Path=SomeCollection}">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding
RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},
Path=DataContext.AllItemsAreChecked}" />
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate DataType="{x:Type local:SomeType}">
<CheckBox Focusable="False" IsChecked="{Binding Path=IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
HorizontalAlignment="Left" VerticalAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="RandomNumber" Width="160">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate DataType="{x:Type local:SomeType}">
<TextBlock Text="{Binding Path=RandomNumber}" TextWrapping="Wrap" HorizontalAlignment="Left" VerticalAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Date" Width="160">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate DataType="{x:Type local:SomeType}">
<TextBlock Text="{Binding Path=Date}" TextWrapping="Wrap" HorizontalAlignment="Left" VerticalAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Time" Width="50">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate DataType="{x:Type local:SomeType}">
<TextBlock Text="{Binding Time}" HorizontalAlignment="Left" VerticalAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
And in viewmodel
SomeCollection binding property is an observablecollection
sometype contains properties like IsSelected , RandomNumber ,Date , Time
for eg:
class ViewModel
{
public ObservableCollection<SomeType> SomeCollection{get;set;}
}
class SomeType
{
public string Date {get;set;}
public string Time {get;set;}
public string RandomNumber {get;set;}
public bool IsSelected {get;set;}
}

Button inside a WPF List view/data grid

I am trying to get the value/ID of the clicked row. If the row is selected, this works fine. But if I just try to click the button inside, the selected customer is null. How do I do Command Parameters here.
I tried this see the answers to the following questions:
ListView and Buttons inside ListView
WPF - Listview with button
Here is the code:
<Grid>
<ListView ItemsSource="{Binding Path=Customers}"
SelectedItem="{Binding Path=SelectedCustomer}"
Width="Auto">
<ListView.View>
<GridView>
<GridViewColumn Header="First Name">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Margin="6,2,6,2">
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Address">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Margin="6,2,6,2">
<Button Content="Address" Command="{Binding Path=DataContext.RunCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
public class VM : ViewModelBase
{
public RelayCommand RunCommand { get; private set; }
private ObservableCollection<Customer> _Customers;
public ObservableCollection<Customer> Customers
{
get { return _Customers; }
set
{
if (value != _Customers)
{
_Customers = value;
RaisePropertyChanged("Customers");
}
}
}
private Customer _SelectedCustomer;
public Customer SelectedCustomer
{
get { return _SelectedCustomer; }
set
{
if (value != _SelectedCustomer)
{
_SelectedCustomer = value;
RaisePropertyChanged("SelectedCustomer");
}
}
}
public VM()
{
Customers = Customer.GetCustomers();
RunCommand = new RelayCommand(OnRun);
}
private void OnRun()
{
Customer s = SelectedCustomer;
}
}
in the OnRun Method, selected Customer is coming in as null. I want the data row(customer) here. How do I do this?
Three possible solutions that you can choose.
Pass current row object as a CommandParameter. In this case, you will modify OnRun method a little. (recommended)
<Button Content="Address" Command="{Binding Path=DataContext.RunCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}" CommandParameter={Binding} />
I think CommandParameter={Binding} works fine because the DataContext of each row is each Customer object.
and OnRun method needs to be modified in order to get a parameter as an argument.
private void OnRun(object o){
if(!(o is Customer)) return;
// Do something
}
Or, Write a little code-behind with SelectionChanged event handling. (not recommended)
Or, Use EventToCommand in MVVM-light toolkit. (not recommended)
CommandParameter does NOT entirely support data binding.
http://connect.microsoft.com/VisualStudio/feedback/details/472932/wpf-commandparameter-binding-should-be-evaluated-before-command-binding

Listview TwoWay Binding With ObservableCollection

:::::::::: ObservableCollection ::::::::::
ObservableCollection<Friend> Friends = new ObservableCollection<Friend> ( );
:::::::::: InitializeFriends ::::::::::
private void InitializeFriends ( )
{
JsonObject _JsonObject = Process . FacebookClient . Get ( "/me/friends" ) as JsonObject;
if ( _JsonObject != null )
{
JsonArray _JsonArray = _JsonObject [ "data" ] as JsonArray;
foreach ( JsonObject Friend in _JsonArray )
{
Friends . Add ( new Friend ( ) { Name = Friend [ "name" ] . ToString ( ) } );
}
}
}
:::::::::: Friend ::::::::::
public class Friend
{
public string Name { get; set; }
public Conversation Chat { get; set; }
public BitmapImage Image { get; set; }
}
:::::::::: ListView ::::::::::
<ListView Name="Panel"
Width="Auto"
Margin="0,200,0,0"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Background="{x:Null}"
BorderThickness="0"
ItemsSource="{Binding Path=Friends,
Mode=TwoWay}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
SelectionMode="Single">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.View>
<GridView ColumnHeaderContainerStyle="{StaticResource hiddenStyle}">
<GridViewColumn Width="200" DisplayMemberBinding="{Binding Path=Name}" />
<GridViewColumn Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<Image Width="30"
Height="30"
Source="{Binding Path=Image}" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
List View Not Binding May Be Binding But Not Updating ?!!!
There is something WRONG
Your Friend class needs to implement the INotifyPropertyChanged interface, so that WPF notifies when a change has been made to that property.
When you have a problem with XAML bindings the first thing to do is run up your program under visual studio and look in your output window for any errors or warnings that mention XAML or Bindings. If there is a problem it will likely show up there.
Your output window can be opened by selecting View->Output from the main menu (or typing Ctrl+W, O). If you're not seeing anything then select Debug in the "Show Output From" combobox.
If you're still not seeing anything then you might need to change your WPF trace level. You can do this by selecting Debug from the main menu and choose “Options and Settings…”. Expand the Debugging node and select “Output Window”. Now in the right hand pane set the “Data Binding” value in "WPF Trace Settings" to at least Warning. You should see some info in the output window next time you run the app under VS.
If you’re still not getting enough information you can alter the trace level (in .Net 3.5 and above) in XAML by adding the System.Diagnostics namespace to your XAML file:
xmlns:diagnostics=”clr-namespace:System.Diagnostics;assembly=WindowsBase”
and setting the trace level in the binding of interest like this:
<ListView Name="Panel"
Width="Auto"
Margin="0,200,0,0"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Background="{x:Null}"
BorderThickness="0"
ItemsSource="{Binding Path=Friends,
Mode=TwoWay,
diagnostics:PresentationTraceSources.TraceLevel=High}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
SelectionMode="Single">
Your Friends collection needs to be exposed as a public property, i.e.:
private readonly ObservableCollection<Friend> _friends = new ObservableCollection<Friend>();
public ObservableCollection<Friend> Friends { get { return _friends; } }
if your datacontext is right your binding should work. btw you just need Mode=OneWay
<ListView Name="Panel" ItemsSource="{Binding Path=Friends, Mode=OneWay}" />
so you should first check your DataContext. the easiest way is to use Snoop. you can also check your visual studio output window to see binding errors
Please add this property to the XAML Window:
DataContext="{Binding RelativeSource={RelativeSource Self}}">

Resources