0

First of all I'm using MVVM light toolkit for building my Windows Store app. So the datacontext will be set in ViewModelLocator. I'm trying to bind command for a button from Hub control's header. Here's my code from XAML which has button:

<DataTemplate x:Key="DataTemplate10">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="700"/>
                    <ColumnDefinition Width="800"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <TextBlock Text="user trading terminal"
                           Width="Auto"
                           Height="70"
                           Grid.Column="0"/>
                <Button Content="sign in" Grid.Column="1"
                        Width="Auto"
                        HorizontalAlignment="Right" Command="{Binding                SignIn}"/>
            </Grid>
        </DataTemplate>

Here's my Hub Control's XAML code from where I'm calling above datatemplate:

<Hub Header="user trading terminal" HeaderTemplate="{StaticResource DataTemplate10}">
            <Hub.Background>
                <ImageBrush Stretch="Fill" ImageSource="ms-appx:///Assets/HubImages/BackGround.jpg"/>
            </Hub.Background>
            <HubSection Header="indices" Width="500" ContentTemplate="{StaticResource DataTemplate1}" x:Name="indicesSection"/>
            <HubSection Header="market movers" Width="530" ContentTemplate="{StaticResource DataTemplate4}"/>
            <HubSection Header="commodities" Width="500" ContentTemplate="{StaticResource DataTemplate6}"/>
            <HubSection Header="currency" Width="500" ContentTemplate="{StaticResource DataTemplate8}"/>

        </Hub>

Following is my ViewModel's code:

public class ApplicationHubViewModel : ViewModelBase
{
 private RelayCommand<object> _signIn;

    /// <summary>
    /// Gets the IndicesTap.
    /// </summary>
    public RelayCommand<object> SignIn
    { 
        get
        {
            return _signIn
                ?? (_signIn = new RelayCommand<object>((t) => SignInProcess(t)));
        }
    }

private void SignInProcess(Object t)
    {
        IsPopUpOpen = true;
        IsHubControlEnabled = false;
    }

} 

Can anyone tell me why I'm not able to call the button's command? Is it because I'm using MVVM light toolkit 5.2.0. Because normally in simple WPF application I'm able to call command without any problems.

OmiH
  • 47
  • 6

2 Answers2

1

This should get you going.

 Command="{Binding SignIn,RelativeSource={RelativeSource   FindAncestor,AncestorType={x:Type Hub}}}
Abhinav Sharma
  • 443
  • 1
  • 4
  • 15
  • If this is what you need,don`t forget to mark as answer.Thanks ;) – Abhinav Sharma Feb 04 '16 at 05:41
  • Already tried with AncestorType & I'm getting error as "The property 'AncestorType' was not found in type 'RelativeSource'". In windows store/phone app Ancestortype property is not available. – OmiH Feb 06 '16 at 03:47
  • Finally got solution on following link: http://stackoverflow.com/questions/15366609/how-to-access-parents-datacontext-in-window-8-store-apps – OmiH Feb 06 '16 at 03:56
0

In Datatemplete button should have following code for command binding:

<DataTemplate x:Key="DataTemplate10">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="700"/>
                    <ColumnDefinition Width="800"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <TextBlock Text="user trading terminal"
                           Width="Auto"
                           Height="70"
                           Grid.Column="0"/>
                <Button Content="sign in" Grid.Column="1"
                        Width="Auto"
                        HorizontalAlignment="Right" Command="{Binding Tag.SignIn, ElementName=mainContainer}"/>
            </Grid>
        </DataTemplate>

The element name should be top level element's name. In my case I have ContentControl as top level element above hub.

        <Hub Header="user trading terminal" HeaderTemplate="{StaticResource DataTemplate10}">
            <Hub.Background>
                <ImageBrush Stretch="Fill" ImageSource="ms-appx:///Assets/HubImages/BackGround.jpg"/>
            </Hub.Background>
            <HubSection Header="indices" Width="500" ContentTemplate="{StaticResource DataTemplate1}" x:Name="indicesSection"/>
            <HubSection Header="market movers" Width="530" ContentTemplate="{StaticResource DataTemplate4}"/>
            <HubSection Header="commodities" Width="500" ContentTemplate="{StaticResource DataTemplate6}"/>
            <HubSection Header="currency" Width="500" ContentTemplate="{StaticResource DataTemplate8}"/>

        </Hub>

    </ContentControl> 

Basically Tag property plays important role while binding.

OmiH
  • 47
  • 6