c# - 如何将图像按钮命令从数据模板选择器绑定(bind)到 View 模型?

标签 c# xamarin xamarin.forms prism syncfusion

我正在与 SyncFusion's TreeView 合作。我有一个带有三个单独的数据模板/自定义 View 单元格的 TreeView。在我的主 XAML 中,我有以下 TreeView 代码,它绑定(bind)到我的 DataTemplates。

SamplePage.xaml

<syncfusion:SfTreeView x:Name="treeView"
       QueryNodeSize="TreeView_QueryNodeSize"
       NodeSizeMode="Dynamic"
       AutoExpandMode="RootNodesExpanded"
       ChildPropertyName="SubFiles"
       ItemsSource="{Binding ImageNodeInfo}" Indentation="0" 
       ItemTemplate="{StaticResource TemplateSelector}">
</syncfusion:SfTreeView>

我现在尝试将我的命令从我的数据模板之一(下面的代码)绑定(bind)到连接到我的页面的 View 模型,但我似乎无法连接我的数据绑定(bind)。大多数在线示例都在 View /viewModel 的同一 xaml 页面上显示数据模板。但是,我的代码使用数据模板选择器,这给我提供了更难的绑定(bind)路径。

示例模板.xaml

<ViewCell.BindingContext>
    <local:SamplePage x:Key="SamplePage">
    </local:SamplePage>
</ViewCell.BindingContext>



   <ImageButton Aspect="AspectFill"
      Grid.Row="0" Grid.Column="3" 
      HorizontalOptions="End" 
      WidthRequest="90" HeightRequest="90" 
      Source="{xaml:ImageResource plus_Icon}"
      Command="{Binding Path=BindingContext.AddAsJobClickedCommand}, Source={x:Reference SamplePage}" 
      CommandParameter="{Binding treeView}" 
                                 />  

最佳答案

试试这个,可能您缺少命令的x:Reference:

<ImageButton Aspect="AspectFill"
             Grid.Row="0" Grid.Column="3" 
             HorizontalOptions="End" 
             WidthRequest="90" HeightRequest="90" 
             Source="{xaml:ImageResource plus_Icon}"
             Command="{Binding Path=BindingContext.AddAsJobClickedCommand, Source={x:Reference treeView}}"
             CommandParameter="{x:Reference treeView}"/>

注意:这里treeView是SfTreeView的x:Name

查找Reference这里

关于c# - 如何将图像按钮命令从数据模板选择器绑定(bind)到 View 模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57599739/

相关文章:

c# - 多点连接 : iOS and Android

c# - Xamarin 表格操作相机给出空异常

c# - Xamarin.Forms 合并字典

android - 需要应用一种样式来摆脱 Entry 上的底线

xcode - 更新 VS 2019 后,Xamarin.ios 应用 native 链接失败

c# - 使用 JSON.net 获取 JToken 的名称/ key

c# - 深度嵌套动态结构的 YamlDotNet 反序列化

c# - 在 MS Word 中获取单元格颜色

c# - .NET Entity Framework Core 2.0 - 代码优先 - 意外的表关系结果

android - Android 上的 MvvmCross 和链接问题