2013-07-31 17 views
0

我有一列中這個按鈕:在WPF Datagrid的具體取決於列的值更改分配的功能和文本按鈕

<DataGridTemplateColumn> 
     <DataGridTemplateColumn.CellTemplate> 
       <DataTemplate> 
        <Button Click="UpdateTopic">Update</Button> 
       </DataTemplate> 
     </DataGridTemplateColumn.CellTemplate> 
</DataGridTemplateColumn> 

但現在我要求無論是在它的文字和功能應用它根據我的專欄中的值進行更改。我怎樣才能做到這一點?

+1

使用'binding'來更改文本並檢查函數內的列值(或使用'binding')。 – Bolu

+0

顯然,您不能綁定函數。 –

回答

0

在後面的代碼或視圖模型創建一個屬性,並將其綁定到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(); 
} 

這種方式,你可以有執行依賴於真正水流的各種職責的點擊處理程序列的nt值。