开发者

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 开发者_StackOverflowto 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 => ...;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜