【发布时间】:2020-03-24 11:00:26
【问题描述】:
我有数据网格。一列是带按钮的:
<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();
}
我知道行和列,但我不知道如何从 WordsDataGrid 中获取我的按钮:
button.Content = new Image
{
Source = new BitmapImage(new Uri(@"/Resources/toupdate.png", UriKind.Relative))
};
编辑:
我添加
Source="{Binding ImageSource}" />
public string ImageSource { get; set; } = @"\Resources\update.png";
先到xaml,再到object,然后我更改字符串。
【问题讨论】:
-
点击按钮时是否要这样做?
-
不,我想在写 WordsDataGrid_CellEditEnding 时这样做。按钮在列索引 = 6 中。所以当行 = 4 时,我想以单元格为例 [4,6] 并在按钮上投射。