【问题标题】:UWP Cannot bind ICommand to User controlUWP 无法将 ICommand 绑定到用户控件
【发布时间】:2018-10-24 16:10:37
【问题描述】:

我目前正在处理一个 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:路径='AddPinCommand' DataItem='PAT.Mobile.UTrakk.UWP.Views.Components.PinInput';目标元素是'PAT.Mobile.UTrakk.UWP.Views.Components.PinInput'(名称='null');目标属性是“SubmitCommand”(输入“ICommand”)

提前致谢,

托马斯 K.T.

【问题讨论】:

    标签: data-binding uwp icommand


    【解决方案1】:

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

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

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

    我不完全理解为什么我必须这样做,因为 CurrentUser.Pin 在没有这个工作的情况下工作得非常好。

    谁能解释一下?

    【讨论】:

    • 它们应该在不同的DataContext中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-11
    • 1970-01-01
    • 2020-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-17
    相关资源
    最近更新 更多