当事件 CellEditEnding wpf 时,数据网格用按钮更改列中的图像

2024-01-02

我有数据网格。一列带有按钮:

<DataGrid x:Name="WordsDataGrid" Grid.Column="0" Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
              AutoGenerateColumns="False" CellEditEnding="WordsDataGrid_CellEditEnding">
        <DataGrid.Columns>
            <DataGridCheckBoxColumn Header="X" Width="10" Binding="{Binding Status}"/>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Visibility="Visible" Height="16" Width="16" Click="Update_Click">
                            <Button.Content>
                                <Image x:Name="KeyName"  Source="Resources/update.png"  />
                            </Button.Content>
                        </Button>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

我还有其他列,其中文本框在 C# 中创建动态。

我想在捕获事件时更改按钮的图像:

private void WordsDataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
    {
        var column = e.Column.DisplayIndex;
        var row = e.Row.GetIndex();
    }

我知道行和列,但我不知道如何从 Word DataGrid 中获取按钮来执行以下操作:

 button.Content = new Image
    {
        Source = new BitmapImage(new Uri(@"/Resources/toupdate.png", UriKind.Relative))
    };

Edit:

I add

Source="{Binding ImageSource}" /> 

public string ImageSource { get; set; } = @"\Resources\update.png"; 

首先是 xaml,其次是对象,然后我更改字符串。


我想通过单击按钮来执行此操作, 您可以使用按钮的单击事件:

private void Update_Click(object sender, EventArgs e)
{
  (sender as Button).Content = new Image
  {
    Source = new BitmapImage(new Uri(@"/Resources/toupdate.png", UriKind.Relative))
  };
}

如果来自数据网格的另一个单元格:

WPF DataGrid:如何访问 DataGridTemplateColumn 特定行中的组合框? https://stackoverflow.com/questions/20255343/wpf-datagrid-how-do-i-access-a-combobox-in-a-specific-row-of-a-datagridtemplate

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当事件 CellEditEnding wpf 时,数据网格用按钮更改列中的图像 的相关文章

随机推荐