我在主页面里嵌套了一个子页面,然后点击子页面的按钮发现调用不了子页面的命令
问题已解决了,感谢各位大佬指教。
按钮要关联上你的子页面的创建函数,才能点击(可以具体一点问题嘛)
“Devil组”引证GPT后的撰写:
你可以使用以下方法:
下面是一个使用命令绑定的示例代码:
<UserControl x:Class="WpfApp1.ChildPage">
<UserControl.Resources>
<local:CustomCommand x:Key="MyCommand"/>
</UserControl.Resources>
<StackPanel>
<Button Command="{StaticResource MyCommand}" Content="Click me"/>
</StackPanel>
</UserControl>
在主页面中:
<Window x:Class="WpfApp1.MainWindow"
xmlns:local="clr-namespace:WpfApp1"
xmlns:child="clr-namespace:WpfApp1.Child"
Title="MainWindow">
<Grid>
<child:ChildPage x:Name="Child"/>
<Button Command="{Binding ElementName=Child, Path=DataContext.MyCommand}" Content="Click child command"/>
</Grid>
</Window>
在这个例子中,子页面中定义了一个名为 MyCommand 的命令,并将其绑定到了一个按钮上。在主页面中,用 ElementName 和 Path 绑定到了子页面的 DataContext 中的 MyCommand 属性,以便在主页面中调用该命令。
第一,你需要等子页面加载完毕,你需要检查子页面的 onload 结果
第二,你需要将子页面封装到一个变量里,并检测子页面是否支持你发送的指令
比如用的是 iframe ,name 是 tabPage
var newPage = open('页面地址','tabPage')
newPage.onoad = function(){主界面按钮变为可用}
主页面按钮.click(function(){newPage.add() // 相应方法调用})