开发者

Binding an ObservableCollection<string> to a ListView

This should be really easy, I just want to update the listview when the user adds something in the textbox to my observable collection.

I don't understand why I can't get this working:

XAML

<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" >
    <Grid>
        <Menu IsMainMenu="True" VerticalAlignment="Top" HorizontalAlignment="Stretch" FlowDirection="LeftToRight">
            <MenuItem Header="File" >
                <MenuItem Header="Open File..">
                </MenuItem>
                <MenuItem Header="Exit">
                </MenuItem>
            </MenuItem>               
        </Menu>
        <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" />
        <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" />

        <ListView Height="338" HorizontalAlignment="Left" Margin="12,97,0,0" Name="listView1" 开发者_StackOverflow社区VerticalAlignment="Top" Width="261" ItemsSource="{Binding SearchTerms}">
        <ListView.View>
            <GridView>
                <GridViewColumn Width="120" Header="Search Term" DisplayMemberBinding="{Binding}" />
            </GridView>
        </ListView.View>
    </ListView>

    </Grid>
</Window>

Code Behind

public partial class MainWindow : Window
{
    public ObservableCollection<string> searchItems = new ObservableCollection<string>();

    public MainWindow()
    {
        InitializeComponent();
    }

    // DEFINE A PROPERTY..
    public ObservableCollection<string> SearchItems
    {
        get { return searchItems; }
    }

    private void AddSearchTerm_Click(object sender, RoutedEventArgs e)
    {
        searchItems.Add(textBoxSearchTerm.Text);
    }
}


How do I reference each item in SearchTerms Here? I can do DisplayMemberPath="{Binding SomeProperty}" But what if I just want the actual object, like a string?

To bind to the item itself, just use {Binding}.

For details, and as a resource, refer to the WPF Binding CheatSheet - this is the first listed binding option.


Tigrans answer is correct with regards to setting the datacontext. This is required to do databinding. There are a number of ways this can be accomplished but the easiest way would be to set the datacontext to your viewmodel (SearchItems).

public MainWindow()
{
    InitializeComponent();
    this.DataContext = SearchItems;
}

Then your binding would look like <ListView ... ItemSource={Binding}> Also you have {Binding SearchTerm} a couple times but i do not see a property that it would be binding to (maybe a typo).

Another option for binding the DataContext is to set it to the control itself:

this.DataContext = this;

This will change your binding expressions to {Binding Path=SearchItems} as this will tell the binding engine to look for the SearchItems property on the current datacontext.

Hope this helps.


To me it seems that the problem is that you're binding to your class field. Define it like a property, instead, and it should work.

EDIT

I noticed that you don't assign a DataContext of you window, which leads to NO binding.

1) Move the collection to separate class (some DataLayer class)

2) Assign to Window's DataContext property an instance of that class. In this case you binding in list view, has to look something like this:

<ListView ... ItemsSource="{Binding Path=SearchTerms}">

After inside your back code use exactly the same instance of the class which was assigned to Window's DataContext property to Add/Remove elements from collection.

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜