简体   繁体   中英

Bug with DataBinding in WPF Host in Winforms?

I've spent far too much time with this and can't find the mistake. Maybe I'm missing something very obvious or I may have just found a bug in the WPF Element Host for Winforms.

I am binding a ListView to a ObeservableList that lives on my ProductListViewModel.

I'm trying to implement searching for the ListView with the general Idea to just change the ObservableList with a new list that is filtered.

Anyway, the ListView Binding code looks like this:

<ListView ItemsSource="{Binding Path=Products}" SelectedItem="{Binding Path=SelectedItem}" SelectionMode="Single">
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"></Setter>
        </Style>
    </ListView.ItemContainerStyle>
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}"></TextBlock>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

And the ViewModel code is as vanilla as it can get:

private ObservableCollection<ProductViewModel> products;
public ObservableCollection<ProductViewModel> Products
{
    get { return products; }
    private set
    {
        if (products != value)
        {
            products = value;
            OnPropertyChanged("Products");
        }
    }
}

Now the problem here: Once I debug into my OnPropertyChanged method, I can see that there are no subscribers to the PropertyChanged event (it's null), so nothing happens on the UI.. I already tried Mode=TwoWay and other Binding modes, it seems I can't get the ListView to subscribe to the ItemsSource...

Can anyone help me with this? I'm just about to forget about the ElemenHost and just do it in Winforms

greetings Daniel

Is there any binding error in the output window?

By the way, you should consider getting the collection view wrapping your products, and then filtering the view, instead of replacing the whole collection.

The code would be something like:

var collectionView = CollectionViewSource.GetDefaultView(Products);
collectionView.Filter += item => ...;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM