我需要在資料模板中系結按鈕的命令,如下所示:
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Content="-" Cursor="Hand" Width="50"
Background="Red" x:Name="removeButton"
Command="{Binding Remove}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
不幸的是,它不起作用。如何在資料模板中的按鈕中系結命令?
我在論壇里找到了這個帖子:
嵌套 WPF 資料模板中的系結
但是回答這個問題的人給出的方法也不起作用。我認為,從那時起,WPF 發生了一些變化,我會感謝您的幫助。
uj5u.com熱心網友回復:
如果Remove在 parent 的視圖模型中定義ListView,您可以使用以下方式系結到它RelativeSource:
Command="{Binding DataContext.Remove,
RelativeSource={RelativeSource AncestorType=ListView}}"
您還可以根據命令屬性的定義位置和應用位置AncestorType來設定Window或。UserControlDataTemplate
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/524078.html
