从另一个数据模板中绑定引用数据模板



我正在尝试绑定来自数据模板之外的数据模板的命令。

命令InfoButtonCommand在我的AppSettingsPageViewModel中,我找不到将其绑定到按钮的方法。

<DataTmplate DataType="{x:Type wizard:AppSettingsPageViewModel}"  x:Name="AppSettingsPageDataTemplate">
<DockPanel>
<Grid>
<dxg:GridControl ItemsSource="{Binding AppSettingsConf, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<dxg:GridColumn FieldName="Key" Header="Key" ReadOnly="True"/>
<dxg:GridColumn FieldName="Value" Header="Value" />
<dxg:GridColumn>
<dxg:GridColumn.CellTemplate>
<DataTemplate>
<!-- The Command Binding -->
<Button Content="ClickMe" Command="{
Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=2, AncestorType={x:Type DataTemplate}}, Path=InfoButtonCommand}">
</Button>
</DataTemplate>
</dxg:GridColumn.CellTemplate>
</dxg:GridColumn>
</dxg:GridControl>
</Grid>
</DockPanel>
</DataTemplate>

我尝试通过数据模板类型进行绑定

<Button Command="{Binding Path=InfoButtonCommand, RelativeSource={RelativeSource AncestorType={x:Type wizard:AppSettingsPageViewModel}}, Mode=TwoWay}">

直接绑定:

<Button Command="{Binding InfoButtonCommand}">

通过名称:

<Button Command="{Binding ElementName=AppSettingsPageDataTemplate, Path=InfoButtonCommand}">

但上述方法似乎都不起作用。任何帮助将不胜感激

在这种情况下,正确的AncestorType将是AncestorType={x:Type dxg:GridControl}

该命令可以通过Path=DataContext.InfoButtonCommand

<DataTemplate DataType="{x:Type wizard:AppSettingsPageViewModel}"  x:Name="AppSettingsPageDataTemplate">
<DockPanel>
<Grid>
<dxg:GridControl ItemsSource="{Binding AppSettingsConf, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<dxg:GridColumn FieldName="Key" Header="Key" ReadOnly="True"/>
<dxg:GridColumn FieldName="Value" Header="Value" />
<dxg:GridColumn>
<dxg:GridColumn.CellTemplate>
<DataTemplate>
<!-- The Command Binding -->
<Button Content="ClickMe" 
Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type dxg:GridControl}}, Path=DataContext.InfoButtonCommand}">
</Button>
</DataTemplate>
</dxg:GridColumn.CellTemplate>
</dxg:GridColumn>
</dxg:GridControl>
</Grid>
</DockPanel>
</DataTemplate>

最新更新