I have datagrid. One column is with button:
<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>
I have other columns with textbox create dynamic in c#.
I want to change image for my button when catch event:
private void WordsDataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
var column = e.Column.DisplayIndex;
var row = e.Row.GetIndex();
}
I know row and column but I dont know how to take my button from WordsDataGrid to do:
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";
First to xaml, secound to object, and I change string.
I you want to do this on click of your button, You can use click event of your button :
private void Update_Click(object sender, EventArgs e)
{
(sender as Button).Content = new Image
{
Source = new BitmapImage(new Uri(@"/Resources/toupdate.png", UriKind.Relative))
};
}
If from another cell of your datagrid :
WPF DataGrid: How do I access a ComboBox in a specific row of a DataGridTemplateColumn?
You should bind the Source
property of the Button
to a source property of your data object and set this property instead of trying to manipulate the UI elements:
private void WordsDataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
var index = ...;
var itemsSource = WordsDataGrid.ItemsSource as IList<YourClass>;
itemsSource[index].Uri = new Uri(@"/Resources/toupdate.png", UriKind.Relative);
}
XAML:
<Image x:Name="KeyName" Source="{Binding Uri}" />
Make sure that YourClass implements INotifyPropertyChanged and raise change notifications.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.