集线器绑定在模板 10 中不起作用



我正在尝试在页面中放置一个集线器,但似乎绑定无法正常工作。我使用了 Template10 汉堡示例,并尝试在主页中放置一个中心。当我测试时,我只是将现有的堆栈面板封装在集线器部分中,但它似乎破坏了按钮单击事件的绑定。

构建时出错

对象引用未设置为对象的实例

<HubSection>
    <DataTemplate>
        <StackPanel Grid.Row="1" VerticalAlignment="Top" Orientation="Horizontal"
        Padding="12,8,0,0">
            <controls:Resizer>
                <TextBox Width="200" MinWidth="200" MinHeight="60"
             Margin="0" Header="Parameter to pass"
             Text="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
             TextWrapping="Wrap">
                    <Interactivity:Interaction.Behaviors>
                        <Behaviors:TextBoxEnterKeyBehavior>
                            <Core:CallMethodAction MethodName="GotoDetailsPage" TargetObject="{Binding}" />
                        </Behaviors:TextBoxEnterKeyBehavior>
                        <Core:EventTriggerBehavior>
                            <Behaviors:FocusAction />
                        </Core:EventTriggerBehavior>
                    </Interactivity:Interaction.Behaviors>
                </TextBox>
            </controls:Resizer>
            <Button Margin="12,0" VerticalAlignment="Bottom"
        Click="{x:Bind ViewModel.GotoDetailsPage}" Content="Submit" />
        </StackPanel>
    </DataTemplate>
</HubSection>

最后设法让按钮单击以使用行为交互来处理绑定。下面是代码。如果其他人有更好的建议,请随时发表评论。

    <Button Margin="12,0" VerticalAlignment="Bottom" Content="Submit" >
        <Interactivity:Interaction.Behaviors>
            <Core:EventTriggerBehavior EventName="Click">
                <Core:CallMethodAction MethodName="GotoDetailsPage" TargetObject="{Binding}" />
            </Core:EventTriggerBehavior>
        </Interactivity:Interaction.Behaviors>
    </Button>

我当然可以向你解释这个问题。由于您使用的是中心控件,并且中心控件使用中心部分,并且与工具箱中的几乎任何其他控件不同,中心部分使用数据模板来绘制其内容,因此数据模板内每个元素的数据上下文不再是外部页面的数据上下文。也就是说,绑定失败是因为绑定到没有所需方法的新类。

至于解决方案,您可以这样做:

<Core:CallMethodAction MethodName="GotoDetailsPage" TargetObject="{Binding ElementName=ViewModel}" />

该语法会将上下文带回页面的视图模型。但是您的解决方案似乎对您有用。我会坚持有效的方法。

祝你好运。

最新更新