[英]Change the assigned function and text to a button in a WPF Datagrid depending on column value
我在列中有此按鈕:
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Click="UpdateTopic">Update</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
但是現在我需要根據其中一列中的值更改其中的文本和應用於它的函數。 我該如何實現?
在您的代碼后面創建一個屬性或查看模型,並將其綁定到Button.Content
屬性:
<Button Click="UpdateTopic" Content={Binding ButtonText}" />
假設隨着列值的更改,您可以訪問綁定到DataGrid
數據對象,請在代碼中編輯與Button.Click
事件鏈接的方法,如下所示:
public void UpdateTopic()
{
if (columnValue == "Something") DoSomething();
else if (columnValue == "SomethingElse") DoSomethingElse();
else if (columnValue == "AnotherThing") DoAnotherThing();
}
這樣,您可以使一鍵式處理程序執行取決於列的當前值的各種任務。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.