access control in datagirdheaderstyle
-
Saturday, September 01, 2012 8:40 AM
hi all
i wrote an datagridheader style
<Grid Height="430" HorizontalAlignment="Left" Margin="14,10,0,0" Name="gridDataGrid" VerticalAlignment="Top" Width="489" Visibility="Hidden" Grid.ColumnSpan="4"> <DataGrid AutoGenerateColumns="True" Name="dataGridBook" ItemsSource="{Binding}" > <DataGrid.ColumnHeaderStyle> <Style TargetType="{x:Type DataGridColumnHeader}"> <Style.Setters> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type DataGridColumnHeader}"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="auto"></ColumnDefinition> <ColumnDefinition Width="*"></ColumnDefinition> <ColumnDefinition Width="*"></ColumnDefinition> </Grid.ColumnDefinitions> <Button Content="{TemplateBinding Content}" Grid.Column="0" Click="GridHeaderButton_Click"></Button> <TextBox x:Name="txt1" Grid.Column="2" Text="{TemplateBinding Content}"></TextBox> <TextBlock Grid.Column="1" Text="{TemplateBinding Content}"></TextBlock> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style.Setters> </Style> </DataGrid.ColumnHeaderStyle> </DataGrid> </Grid>i want access txt1 in my code.
i try alot but dont access that.
any one can help?
thankful.
All Replies
-
Saturday, September 01, 2012 8:56 AM
Hi rastinrastini,
You can go through below link
http://msdn.microsoft.com/en-us/library/bb613586.aspx
http://stackoverflow.com/questions/820201/how-to-access-a-wpf-control-located-in-a-controltemplate
or here is the sample code in which i am accessing control which is inside the ControlTemplate from code behind
You can create an application with name "WpfApplication28" and copy/paste below code
<Window x:Class="WpfApplication28.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<ControlTemplate x:Key="MyTemplate" TargetType="{x:Type Button}">
<Border x:Name="border" Background="Aqua">
<ContentPresenter VerticalAlignment="Center"
HorizontalAlignment="Center"/>
</Border>
</ControlTemplate>
</Window.Resources>
<Grid>
<Button Content="Hello world" x:Name="myButton" Template="{StaticResource MyTemplate}"
VerticalAlignment="Top"/>
<Button Content="Click" VerticalAlignment="Bottom" Click="Button_Click"/>
</Grid>
</Window>
.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication28
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Border b = (Border)myButton.Template.FindName("border", myButton);
b.Background = Brushes.Gray;
}
}
}Hope it will help.
Please let me know if you have any query.
Regards
Ashish
-------------------------------------------------------------------------
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
- Edited by agrawal.ashish Saturday, September 01, 2012 8:58 AM
-
Saturday, September 01, 2012 9:51 AM
thank you for your answer.
my problem is i cant find control template.
it is inside setter in style of datagridheaderstyle that i can not access it.
i want know how can i access it.
thankful.
-
Saturday, September 01, 2012 4:22 PM
Hi rastinrastini,
I placed template inside the style in my sample code, still i can access the border
XAML
<Window x:Class="WpfApplication28.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<Style x:Key="myStyle" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="border" Background="Aqua">
<ContentPresenter VerticalAlignment="Center"
HorizontalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid x:Name="myGrid">
<Button Content="Hello world" x:Name="myButton"
Style="{StaticResource myStyle}"
VerticalAlignment="Top"/>
<Button Content="Click" VerticalAlignment="Bottom" Click="Button_Click"/>
</Grid>
</Window>
.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication28
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Border b = (Border)myButton.Template.FindName("border", myButton);
if (b != null)
{
b.Background = Brushes.Gray;
}
}
}
}can you please share your cs code to find out the solution.
Regards
Ashish
-------------------------------------------------------------------------
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
-
Saturday, September 01, 2012 7:22 PM
<Grid Height="430" HorizontalAlignment="Left" Margin="14,10,0,0" Name="gridDataGrid" VerticalAlignment="Top" Width="489" Visibility="Hidden" Grid.ColumnSpan="4"> <DataGrid AutoGenerateColumns="True" Name="dataGridBook" ItemsSource="{Binding}" > <DataGrid.ColumnHeaderStyle> <Style TargetType="{x:Type DataGridColumnHeader}"> <Style.Setters> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type DataGridColumnHeader}"> <Grid x:Name="headerGrid"> <Grid.ColumnDefinitions> <ColumnDefinition Width="auto"></ColumnDefinition> <ColumnDefinition Width="*"></ColumnDefinition> <ColumnDefinition Width="*"></ColumnDefinition> </Grid.ColumnDefinitions> <Button Content="{TemplateBinding Content}" Grid.Column="0" Click="GridHeaderButton_Click"></Button> <TextBox x:Name="txt1" Grid.Column="2" Text="{TemplateBinding Content}"></TextBox> <TextBlock Grid.Column="1" Text="{TemplateBinding Content}"></TextBlock> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style.Setters> </Style> </DataGrid.ColumnHeaderStyle> </DataGrid> </Grid>
very thankful for your answer.
my code for access is this but get null value:
var _gridTemplate = dataGridBook.Template.FindName("headerGrid", dataGridBook)
thankful.
-
Sunday, September 02, 2012 1:30 PM
hi all
Setter s1 = (Setter)dataGridBook.ColumnHeaderStyle.Setters[0]; ControlTemplate _controlTemplate = (ControlTemplate)s1.Value;
with this code i get control template but when i use this
var _txt = _controlTemplate.FindName("headerGrid", dataGridBook);
i get this error
"This operation is valid only on elements that have this template applied."
can anyone help me?
thankful.
-
Monday, September 03, 2012 11:29 AMModerator
Hi rastinrastini,
the key is to get DataGridColumnHeadersPresenter firstly, I suggest you learn more about WPF DataGrid template struct, you could use below code to get the control you want:
DataGridColumnHeadersPresenter chp = null; Control sv = dataGridBook.Template.FindName("DG_ScrollViewer", dataGridBook) as Control; if (sv != null) { chp = sv.Template.FindName("PART_ColumnHeadersPresenter", sv) as DataGridColumnHeadersPresenter; if (chp != null) { TextBox tb= ((DataGridColumnHeader)chp.ItemContainerGenerator.ContainerFromIndex(0)).Template.FindName("txt1", (DataGridColumnHeader)chp.ItemContainerGenerator.ContainerFromIndex(0)) as TextBox; } }You could change the ContainerFromIndex(0) to other column index.
best regards,
Sheldon _Xiao[MSFT]
MSDN Community Support | Feedback to us
Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Marked As Answer by rastinrastini Tuesday, September 04, 2012 4:51 AM
-
Monday, September 03, 2012 4:04 PM
very thankful for your answer.
thankful.
-
Monday, September 03, 2012 4:10 PM
i see it is for .net 4 and 4.5.
then if we use 3.5 how can access these contents?
thankful.
-
Tuesday, September 04, 2012 3:05 AMModerator
there are no different between 3.5 and 4.0, have you tried it by yourself?
I suggest you try it.
the only difference between 3.5 and 4.0 is there is no DataGrid in 3.5, you should use Toolkit datagrid instead.
I said many times about WPF VisualTree, you can learn it, you could get any control you want by VisualTree.
Sheldon _Xiao[MSFT]
MSDN Community Support | Feedback to us
Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
Tuesday, September 04, 2012 4:51 AM
very thankful.

