[英]Change the assigned function and text to a button in a WPF Datagrid depending on column value
I have this button inside a column: 我在列中有此按钮:
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Click="UpdateTopic">Update</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
But now I require that both the text in it and the function applied to it to change according to the value in one of my column. 但是现在我需要根据其中一列中的值更改其中的文本和应用于它的函数。 How can I achieve this?
我该如何实现?
Create a property in your code behind or view model and bind it to the Button.Content
property: 在您的代码后面创建一个属性或查看模型,并将其绑定到
Button.Content
属性:
<Button Click="UpdateTopic" Content={Binding ButtonText}" />
Assuming that you have access to your data objects bound to the DataGrid
as the column value changes, edit the method in your code behind that is linked to the Button.Click
event to something like this: 假设随着列值的更改,您可以访问绑定到
DataGrid
数据对象,请在代码中编辑与Button.Click
事件链接的方法,如下所示:
public void UpdateTopic()
{
if (columnValue == "Something") DoSomething();
else if (columnValue == "SomethingElse") DoSomethingElse();
else if (columnValue == "AnotherThing") DoAnotherThing();
}
This way, you can have one click handler that performs a variety of duties dependant on the current value of the column. 这样,您可以使一键式处理程序执行取决于列的当前值的各种任务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.