简体   繁体   English

根据列值将分配的功能和文本更改为WPF Datagrid中的按钮

[英]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.

相关问题 根据值更改 WPF DataGrid 列上的图像 - Change image on WPF DataGrid column depending on value WPF DataGrid的结构 - 根据值更改单元格 - Structure of WPF DataGrid - change cells depending on value 如何根据另一列的值更改 DataGrid 中按钮的前景色? WPF C# - How to change the Foreground color of a button in a DataGrid depending on values from another column? WPF c# WPF数据绑定Datagrid根据数据类型更改水平列方向 - WPF databound Datagrid change horizontal column orientation depending on data type 如何根据绑定值更改WPF数据网格行的图像 - How to change the image of WPF datagrid row depending on binding value 具有可变列数的WPF Datagrid并根据值更改背景色 - WPF Datagrid with variable number of columns AND change background color depending on value WPF 根据绑定值更改数据网格行的 PackIcon - WPF Change the PackIcon of datagrid row depending on binding value 根据列名称更改列中行中文本的所有颜色(datagrid) - Change all the colours of text in rows in a column depending on the column name (datagrid) 事件 CellEditEnding wpf 时,Datagrid 使用按钮更改列中的图像 - Datagrid change image in column with button when event CellEditEnding wpf 带有标题文本的列标题中的 WPF 数据网格按钮 - C# - WPF datagrid button in column header with header text - c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM