Bind to property of DataContext of another user control

427 views Asked by At

I have Player inside data context of user control. I want to Bind ListView from main window to property of Player : Tracks.

MainWindow:

<Window 
    xmlns:local="clr-namespace:PlayerApp"
    xmlns:player="clr-namespace:Player"
    ... >

<local:PlayerControl x:Name="PlayerControl" />

<ListBox x:Name="TrackList"
         ItemsSource="{Binding DataContext.(player:Player.Tracks) , <!--error here-->
                               Source={x:Reference PlayerControl} , 
                               Mode=OneWay}" 
         SelectedIndex="{Binding DataContext.(player:Player.SelectedTrack) , <!--no error-->
                                 Source={x:Reference PlayerControl}}" 
         SelectionMode="Single"/>

UserControl

<UserControl x:Class="PlayerApp.PlayerControl" ... >
    <UserControl.DataContext>
        <player:Player x:Name="MyPlayer"/> <!--player is created here-->
    </UserControl.DataContext>

These are the properties inside Player class.

public ObservableCollection<Track> Tracks { get; } = new ObservableCollection<Track>();
public int SelectedTrack { get; set; }

The binding does work fine only at runtime. I.e in designer part I'm getting

Ivalid Markup : Check the Error List for more information

Therefore I cant see MainWindow in designer. I get this error in XAML.

The member "Tracks" is not recognized or is not accessible

The program compiles fine. Runs fine, even binding works fine but this error is stuck.

I tried restart .net. clean solution. close all windows and open it again. but error is there. is this bug or something i did wrong? what could be the cause?

Is there any other way to bind like this without getting error?

0

There are 0 answers