Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding an ObservableCollection<string> to a ListView
    primarykey
    data
    text
    <p>This should be really easy, I just want to update the listview when the user adds something in the textbox to my observable collection.</p> <p>I don't understand why I can't get this working:</p> <h2>XAML</h2> <pre><code>&lt;Window x:Class="MyStuff.WPF.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="486" Width="540" WindowStyle="SingleBorderWindow" ResizeMode="NoResize" &gt; &lt;Grid&gt; &lt;Menu IsMainMenu="True" VerticalAlignment="Top" HorizontalAlignment="Stretch" FlowDirection="LeftToRight"&gt; &lt;MenuItem Header="File" &gt; &lt;MenuItem Header="Open File.."&gt; &lt;/MenuItem&gt; &lt;MenuItem Header="Exit"&gt; &lt;/MenuItem&gt; &lt;/MenuItem&gt; &lt;/Menu&gt; &lt;TextBox Height="36" HorizontalAlignment="Left" Margin="12,27,0,0" Name="textBoxSearchTerm" VerticalAlignment="Top" Width="414" FontSize="24" MaxLength="80" AcceptsReturn="False" Background="#FFFFFFE8" Text="asdfasdf" FontWeight="Bold" FontFamily="Calibri" FontStretch="Normal" /&gt; &lt;Button Content="Add" Height="36" HorizontalAlignment="Left" Margin="435,27,0,0" VerticalAlignment="Top" Width="68" FontSize="20" FontWeight="Normal" FontFamily="Calibri" Click="AddSearchTerm_Click" /&gt; &lt;ListView Height="338" HorizontalAlignment="Left" Margin="12,97,0,0" Name="listView1" VerticalAlignment="Top" Width="261" ItemsSource="{Binding SearchTerms}"&gt; &lt;ListView.View&gt; &lt;GridView&gt; &lt;GridViewColumn Width="120" Header="Search Term" DisplayMemberBinding="{Binding}" /&gt; &lt;/GridView&gt; &lt;/ListView.View&gt; &lt;/ListView&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <h2>Code Behind</h2> <pre><code>public partial class MainWindow : Window { public ObservableCollection&lt;string&gt; searchItems = new ObservableCollection&lt;string&gt;(); public MainWindow() { InitializeComponent(); } // DEFINE A PROPERTY.. public ObservableCollection&lt;string&gt; SearchItems { get { return searchItems; } } private void AddSearchTerm_Click(object sender, RoutedEventArgs e) { searchItems.Add(textBoxSearchTerm.Text); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload