Reputation: 213
I have a DataGridTemplateColumn whose header basically consists of StackPanel which contains a Button and TextBlock. I would want to access the Cell content or DataGridCell when the datagridrow is loaded.
My one of the datagridcolumn looks like this:
<DataGridTemplateColumn>
<DataGridTemplateColumn.HeaderTemplate >
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Click="HeaderButtonClick">
<Button.Content>
<Path Stretch="Fill" Fill="Black" Stroke="{x:Null}" StrokeThickness="0.5"
Data="M3.875,0 L5.125,0 5.125,3.875 9,3.875 9,5.125 5.125,5.125 5.125,9 3.875,9 3.875,5.125 0,5.125 0,3.875 3.875,3.875 3.875,0 z" />
</Button.Content>
</Button>
<TextBlock Text="Fund" Margin="20,0,0,0"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Fund}" Margin="20,0,0,0"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
and the datagrid_loadingrow event looks like
var dg = sender as DataGrid;
if (dg != null)
{
foreach (var item in dg.Columns)
{
DataGridCell cell1 = item.GetCellContent(row) as DataGridCell;
}
}
Can anyone help me with this?
Upvotes: 4
Views: 4786
Reputation: 472
Here is answer.How you would access template column content using code.If you want to access any type of Control content e.g Button content ,TextBlock content etc.Then you create load event for those Controls.Then go to load event code.In load event you would be access Content of those Controls.
I am going to access the Button child of Stack Panel.
So follow this code.
Xaml:
<DataGridTemplateColumn>
<DataGridTemplateColumn.HeaderTemplate >
<DataTemplate>
<StackPanel Orientation="Horizontal" Loaded="StackPanel_Loaded">
<Button Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Click="HeaderButtonClick">
<Button.Content>
<Path Stretch="Fill" Fill="Black" Stroke="{x:Null}" StrokeThickness="0.5"
Data="M3.875,0 L5.125,0 5.125,3.875 9,3.875 9,5.125 5.125,5.125 5.125,9 3.875,9 3.875,5.125 0,5.125 0,3.875 3.875,3.875 3.875,0 z" />
</Button.Content>
</Button>
<TextBlock Text="Fund" Margin="20,0,0,0"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Fund}" Margin="20,0,0,0"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
Code Behind .cs
private void StackPanel_Loaded(object sender, RoutedEventArgs e)
{
StackPanel stack = sender as StackPanel;
Button button = stack.Children.OfType<Button>().FirstOrDefault();
button.Content = "Change Content";
}
In my situation I want to access button in Template Column.I used this code XAML
<DataGridTemplateColumn x:Name="deleteDG" Header="Delete" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate x:Uid="deletetemp" >
<Button Click="deleteBtn_Click" Loaded="Button_Loaded" Width="95" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Background="White" Content="Delete"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Code Behind .cs
private void Button_Loaded(object sender, RoutedEventArgs e)
{
Button button = sender as Button;
button.Content = "Change COntent";
}
Upvotes: 2
Reputation: 69959
This is WPF, not WindowsForms. When using WPF, we data bind to UI control properties, rather than attempting to programmatically set these values. When data binding, you already have access to the data in your code behind/view model, so there really is no need to attempt to access it from the UI controls.
If you look at the DataGridTemplateColumn
Class page on MSDN, you'll see this example:
<Grid>
<Grid.Resources>
<!--DataTemplate for Published Date column defined in Grid.Resources. PublishDate is a property on the ItemsSource of type DateTime -->
<DataTemplate x:Key="DateTemplate" >
<StackPanel Width="20" Height="30">
<Border Background="LightBlue" BorderBrush="Black" BorderThickness="1">
<TextBlock Text="{Binding PublishDate, StringFormat={}{0:MMM}}" FontSize="8" HorizontalAlignment="Center" />
</Border>
<Border Background="White" BorderBrush="Black" BorderThickness="1">
<TextBlock Text="{Binding PublishDate, StringFormat={}{0:yyyy}}" FontSize="8" FontWeight="Bold" HorizontalAlignment="Center" />
</Border>
</StackPanel>
</DataTemplate>
<!--DataTemplate for the Published Date column when in edit mode. -->
<DataTemplate x:Key="EditingDateTemplate">
<DatePicker SelectedDate="{Binding PublishDate}" />
</DataTemplate>
</Grid.Resources>
<DataGrid Name="DG1" ItemsSource="{Binding}" AutoGenerateColumns="False" >
<DataGrid.Columns>
<!--Custom column that shows the published date-->
<DataGridTemplateColumn Header="Publish Date" CellTemplate="{StaticResource DateTemplate}" CellEditingTemplate="{StaticResource EditingDateTemplate}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
Notice the Binding
s... for example, we can see this:
<DatePicker SelectedDate="{Binding PublishDate}" />
This value can be accessed directly from the PublishDate
property of the data bound object, rather than from the DataGrid
:
DateTime publishDate = DataBoundCollection.ElementAt(relevantRowIndex).PublishDate;
You can find out more about data binding from the Data Binding Overview page on MSDN.
Upvotes: 2