0

I have a usercontrol containing some buttons and a ListView.

I want my custom control to have an ItemsSource property that binds to the listviews itemsource directly.

MyControl.xaml.cs

public partial class MyControl : UserControl
{

    public static DependencyProperty ItemsSourceProperty =
              ListView.ItemsSourceProperty.AddOwner(typeof(AddFilesControl));

    public ObservableCollection<DocumentFile> ItemsSource
    {
        get { return (ObservableCollection<DocumentFile>)GetValue(ItemsSourceProperty); }
        set { SetValue(ItemsSourceProperty, value); }
    }
}

MyControl.xaml

<UserControl x:Class="[...].MyControls.MyControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008">
    <Grid>
        <ListView>
           <ListView.ItemTemplate>
              [...]
           </ListView.ItemTemplate>
        </ListView>
    </Grid>
</UserControl>

MyViewModel.cs (set as the DataSource of MyWindow containing only MyControl)

public class MyViewModel : INotifyPropertyChanged
{
    public ObservableCollection<DocumentFile> DefaultList { get; set; }
}

When debugging no items are displayed, but there are items in the ViewModel.

The Binding seems to be correct.

<custom:MyControl ItemsSource="{Binding DefaultList}" />

What is wrong here ?

Felix D.
  • 4,811
  • 8
  • 38
  • 72
  • I don't get why this should be offtopic. The problem is clear and so is the code I provided. (IMO) .. ?! – Felix D. May 28 '18 at 12:25
  • I asked a similar question a while ago, check it out: https://stackoverflow.com/questions/50352908/how-to-properly-expose-properties-of-internal-controls – Ruben Bohnet May 28 '18 at 12:25
  • @RubenBohnet thanks. I've seen that. It's working for very well for the `SelectedItem`-Property. I got no idea why `ItemsSource` is not working. – Felix D. May 28 '18 at 12:27
  • the code you provided, isn't a [mcve] - it is not complete. where DP is used to display items? also in `ItemsSoure="{Binding DefaultList}"` property name is ItemsSoure, not ItemsSource – ASh May 28 '18 at 12:48
  • @Ash that's just a spelling mistake. IMO it contains everything one could need to reproduce the issue. True - I could add some more xaml and a dummy viewmodel. But IMO it't too obvious what it would look like. – Felix D. May 28 '18 at 12:58
  • @FelixD., I repeat my previous question "where DP ItemsSource is used to display items?". It is declared and assigned, but not connected to view element – ASh May 28 '18 at 13:06
  • @Ash Well I guess I did not implement it in the right way. the above is all I got. maybe thats already the source of the problem. Doesn't `ListView.ItemsSourceProperty.AddOwner` already links the ItemsSource to the listview in my control ? – Felix D. May 28 '18 at 13:11
  • `AddOwner` method has entirely different purpose. Binding can link the ItemsSource to the listview. – ASh May 28 '18 at 13:13
  • @ASh could you provide an example ? I'm kinda confused how to bind and not use the datacontext that is provided for the custom-controls-parent ?! – Felix D. May 28 '18 at 13:15
  • 1
    As a note, a property of type `ObservableCollection` should not be called ItemsSource. It should just be `DocumentFiles` or something like that. You can't assign any kind of items. – Clemens May 28 '18 at 16:48

1 Answers1

2

ListView element which is a part of MyControl is not connected to MyControl.ItemsSource

that can be fixed by creating a binding:

<UserControl x:Class="[...].MyControls.MyControl"
             x:name="myControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008">
    <Grid>
        <ListView ItemsSource="{Binding ItemsSource, ElementName=myControl}">

        </ListView>
    </Grid>
</UserControl>

DP.AddOwner() method doesn't create a binding. ItemsSourceProperty DP is declared by ItemsControl class. AddOwner isn't aware about ListView in MyControl. How can it bind them together?

ASh
  • 34,632
  • 9
  • 60
  • 82