Search code examples
mvvmcustom-controlssilverlight-5.0lightswitch-2012

Custom Silverlight Control Binding Issue Lightswitch


I've been fighting with a certain issue with custom control binding i cant seem to be able to solve.

I'm trying to bind a wizard control i downloaded from the web to one of my screens, but for some reason the bindings wont pull through.

I have narrowed down the issue to one line of code that is in fact the one that causes the problems.

private readonly MainPageViewModel vm;
    public MainPage()
    {
        InitializeComponent();
        vm = new MainPageViewModel();
        DataContext = vm;
    }

it is overriding the datacontext (using MVVM pattern to open the main page VM).

when i remove the datacontext override then my binding works perfectly but all other control functionalities are not working properly.

now it is clear i might be doing something wrong or i am missing something, with your help and expertise im hoping to resove this.

Here's part of the XAML of the custom control when im binding to my screen, could it be that with the datacontext override happening i need to add Source to my binding, if so what is the source?

<controls:TabItem Header="introduction"
                              Tag="WELCOME TO THE INSERT PRODUCT WIZARD"
                              Visibility="Collapsed" >
                <StackPanel>
                    <TextBox x:Name="txtTest" Width="300" Text="{Binding Path=Screen.intBindTest, Mode=TwoWay}"/>
                    <ComboBox  ItemsSource="{Binding Screen.vw_COUNTRIES, Mode=OneWay}" 
                               SelectedItem="{Binding Screen.vw_COUNTRIES.SelectedItem, Mode=TwoWay}"
                               DisplayMemberPath="COUNTRYNAME"/>
                </StackPanel>


            </controls:TabItem>

My issue is not how do i bind silverlight custom controls, my problem is when I explicitly assign a datacontext to be the ViewModel then the LightSwitch bindings are not working but if i remove it then Lightswitch can bind fine but the rest of the control is broken.

Besically what i need is how to explicitly assign lightswitch datacontext into my textbox from XAML or any other way.

thanks alot in advance.


Solution

  • I have found the solution to my issue.

    a user control only gets the screen data context if you dont explicitly specify a context for it, it will get it by default. but when i explicitly override it with my VM then the context isnt coming from the screen anymore.

    the solution was after digging inside Microsoft.LightSwitch.Client.Internal DLL and looking for the parent control for custom control is LS.

    after that i added the DLL to my solution so i could reference that DLL and use the object type, and added a datacontext binding like so.

    <TextBox Grid.Row="1" x:Name="txtTest" Width="300" Text="{Binding Path=Screen.intBindTest, Mode=TwoWay}"
                                 DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
                            AncestorType=LS:ScreenCustomContentControl ,AncestorLevel=1}, Path=DataContext}"/>
                            <ComboBox Grid.Row="2"  Name="cmbTest" ItemsSource="{Binding Screen.vw_COUNTRIES, Mode=TwoWay}" 
                                   SelectedItem="{Binding Screen.vw_COUNTRIES.SelectedItem, Mode=TwoWay}"
                                   DisplayMemberPath="COUNTRYNAME"
                                   DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
                            AncestorType=LS:ScreenCustomContentControl ,AncestorLevel=1}, Path=DataContext}"/>
    

    That solved my problem.