UWP无法将ICommand绑定到用户控件

时间:2018-10-24 16:10:37

标签: data-binding uwp icommand

我目前正在从事UWP项目,并且试图将当前页面中的某些命令绑定到用户控件。

虽然其他所有属性似乎都已正确绑定,但该命令无效,因此无法正常工作。

您有什么想法吗?

这是我的代码(主页):

<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>

查看模型:

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

用户控制:

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);
            }
        }

注意:

在错误日志中,我有: 错误:BindingExpression路径错误:在'PAT.Mobile.UTrakk.UWP.Views.Components.PinInput'上找不到'AddPinCommand'属性。 BindingExpression:Path ='AddPinCommand'DataItem ='PAT.Mobile.UTrakk.UWP.Views.Components.PinInput';目标元素是'PAT.Mobile.UTrakk.UWP.Views.Components.PinInput'(Name ='null');目标属性为“ SubmitCommand”(类型为“ ICommand”)

预先感谢

Thomas K. T。

1 个答案:

答案 0 :(得分:0)

好吧,这很奇怪,但这似乎可以解决问题:

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

With“ curPage” =当前页面的名称。

我不完全理解为什么必须这样做,因为CurrentUser.Pin在没有这种解决方法的情况下工作得很好。

有人可以解释吗?

相关问题