Search code examples
data-bindinguwpicommand

UWP Cannot bind ICommand to User control


I am currently working on a UWP project and I am trying to bind some command from the current page to an usercontrol.

While every other properties seems to be correctly bound, the command is not and thus is not working.

Do you have any idea from where it might come ?

Here is my code (main page):

<Grid Visibility="{Binding LoginStep, Converter={StaticResource LogConverter}, ConverterParameter='InputPin'}"
          Height="450px"  Width="280px">
            <PIN:PinInput Pin="{Binding CurrentUser.Pin, Mode=TwoWay}" x:Uid="btnPin"  SubmitCommand="{Binding AddPinCommand, Mode=TwoWay}"></PIN:PinInput>

</Grid>

View model:

private ICommand _addPinCommand;
        public ICommand AddPinCommand
        {
            get
            {
                return _addPinCommand ?? (_addPinCommand = new CommandBase((o) =>
                {
                    this.LoginStep = LoginStep.ChoseCompany;
                }));
            }
        }

User control:

DataContext="{Binding RelativeSource={RelativeSource Self}}"

 <Grid Grid.Row="3">
            <Grid.ColumnDefinitions>
                <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Button  Style="{StaticResource btnStyle}"  Command="{Binding SubmitCommand, Mode=TwoWay}">
                <StackPanel>
                    <Border Style="{StaticResource btnInnerStyle}" Width="100px" CornerRadius="10">
                        <TextBlock Text="{Binding SubmitButtonText}" Style="{StaticResource btnText}"></TextBlock>
                    </Border>
                </StackPanel>
            </Button>
        </Grid>




 public static readonly DependencyProperty submitCommandProperty = DependencyProperty.Register("SubmitCommand", typeof(ICommand), typeof(PinInput), null);



public ICommand SubmitCommand
        {
            get
            {
                return (ICommand)GetValue(submitCommandProperty);
            }
            set
            {
                SetValue(submitCommandProperty, value);
            }
        }

Note:

in the error log I have: Error: BindingExpression path error: 'AddPinCommand' property not found on 'PAT.Mobile.UTrakk.UWP.Views.Components.PinInput'. BindingExpression: Path='AddPinCommand' DataItem='PAT.Mobile.UTrakk.UWP.Views.Components.PinInput'; target element is 'PAT.Mobile.UTrakk.UWP.Views.Components.PinInput' (Name='null'); target property is 'SubmitCommand' (type 'ICommand')

Thanks in advance,

Thomas K. T.


Solution

  • Ok this one was weird but here is what seems to do the trick:

    <PIN:PinInput SubmitCommand="{Binding DataContext.AddPinCommand, ElementName=curPage}" Pin="{Binding CurrentUser.Pin, Mode=TwoWay}" x:Uid="btnPin"  ></PIN:PinInput>
    

    With "curPage" = name of the current page.

    I do not fully understand why I have to do this, since CurrentUser.Pin is working perfectly fine without this work around.

    Can someone explain ?