UWP 无法将 ICommand 绑定到用户控件
UWP Cannot bind ICommand to User control
我目前正在开发一个 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.
好吧,这个很奇怪,但这似乎可以解决问题:
<PIN:PinInput SubmitCommand="{Binding DataContext.AddPinCommand, ElementName=curPage}" Pin="{Binding CurrentUser.Pin, Mode=TwoWay}" x:Uid="btnPin" ></PIN:PinInput>
使用 "curPage" = 当前页面的名称。
我不完全理解为什么我必须这样做,因为 CurrentUser.Pin 在没有这种解决方法的情况下工作得很好。
谁能解释一下?
我目前正在开发一个 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.
好吧,这个很奇怪,但这似乎可以解决问题:
<PIN:PinInput SubmitCommand="{Binding DataContext.AddPinCommand, ElementName=curPage}" Pin="{Binding CurrentUser.Pin, Mode=TwoWay}" x:Uid="btnPin" ></PIN:PinInput>
使用 "curPage" = 当前页面的名称。
我不完全理解为什么我必须这样做,因为 CurrentUser.Pin 在没有这种解决方法的情况下工作得很好。
谁能解释一下?