我正在尝试从DataTemplate外部绑定来自DataTemplate的命令。
命令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>
我尝试通过DataTemplates类型进行绑定
<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}">
但是以上方法似乎都不起作用。任何帮助将不胜感激
答案 0 :(得分:1)
在这种情况下,正确的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>