How to set stretching for TextBox in column header in code-behind?
up vote
2
down vote
favorite
I am trying to write simple multi-filtering for datagrid. The concept is to add TextBox to each column header in data grid. User may type some value in each TextBox and then rows in DataGrid will be filtered.
Columns are generated dynamiclly. I am doing this in code-behind.
To add the TextBox to column header I have created new DataTemplate and assigned that to HeaderTemplate of column as below:
DataGridTextColumn column = new DataGridTextColumn();
FrameworkElementFactory textBox = new FrameworkElementFactory(typeof(TextBox));
textBox.SetValue(NameProperty, "exemplaryName");
textBox.SetValue(HorizontalAlignmentProperty, HorizontalAlignment.Stretch);
FrameworkElementFactory textBlock = new FrameworkElementFactory(typeof(TextBlock));
textBlock.SetValue(TextBlock.TextProperty, "exemplaryName");
FrameworkElementFactory stackPanel = new FrameworkElementFactory(typeof(StackPanel));
stackPanel.AppendChild(textBox);
stackPanel.AppendChild(textBlock);
DataTemplate headerTemplate = new DataTemplate();
headerTemplate.VisualTree = stackPanel;
column.HeaderTemplate = headerTemplate;
temp.dataGrid.Columns.Add(column);
Recent view of DataGrid column headers:
What is my problem. When I am setting TextBox HorizontalAlignment value to "Stretch" nothing happened. As you can see in the picture above columns have some specific width (which value is equal to Auto) but TextBox HorizontalAlignment remains with its default value.
My goal is to have this TextBoxes stretched even when user will resize a column (dragging left or right).
How to set TextBox value HorizontalAlignment to stretch? Where I am missing something? Does any additional binding or some other mechanism is necessary to obtain stretching when width of the column is changing?
c# wpf wpf-controls code-behind
New contributor
add a comment |
up vote
2
down vote
favorite
I am trying to write simple multi-filtering for datagrid. The concept is to add TextBox to each column header in data grid. User may type some value in each TextBox and then rows in DataGrid will be filtered.
Columns are generated dynamiclly. I am doing this in code-behind.
To add the TextBox to column header I have created new DataTemplate and assigned that to HeaderTemplate of column as below:
DataGridTextColumn column = new DataGridTextColumn();
FrameworkElementFactory textBox = new FrameworkElementFactory(typeof(TextBox));
textBox.SetValue(NameProperty, "exemplaryName");
textBox.SetValue(HorizontalAlignmentProperty, HorizontalAlignment.Stretch);
FrameworkElementFactory textBlock = new FrameworkElementFactory(typeof(TextBlock));
textBlock.SetValue(TextBlock.TextProperty, "exemplaryName");
FrameworkElementFactory stackPanel = new FrameworkElementFactory(typeof(StackPanel));
stackPanel.AppendChild(textBox);
stackPanel.AppendChild(textBlock);
DataTemplate headerTemplate = new DataTemplate();
headerTemplate.VisualTree = stackPanel;
column.HeaderTemplate = headerTemplate;
temp.dataGrid.Columns.Add(column);
Recent view of DataGrid column headers:
What is my problem. When I am setting TextBox HorizontalAlignment value to "Stretch" nothing happened. As you can see in the picture above columns have some specific width (which value is equal to Auto) but TextBox HorizontalAlignment remains with its default value.
My goal is to have this TextBoxes stretched even when user will resize a column (dragging left or right).
How to set TextBox value HorizontalAlignment to stretch? Where I am missing something? Does any additional binding or some other mechanism is necessary to obtain stretching when width of the column is changing?
c# wpf wpf-controls code-behind
New contributor
It looks like your text box ends up in another container that has exactly that size. Stretch doesn't mean "Push" but rather "occupy what you can". If you snoop this you will find that it does indeed stretch.
– Rob
2 days ago
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I am trying to write simple multi-filtering for datagrid. The concept is to add TextBox to each column header in data grid. User may type some value in each TextBox and then rows in DataGrid will be filtered.
Columns are generated dynamiclly. I am doing this in code-behind.
To add the TextBox to column header I have created new DataTemplate and assigned that to HeaderTemplate of column as below:
DataGridTextColumn column = new DataGridTextColumn();
FrameworkElementFactory textBox = new FrameworkElementFactory(typeof(TextBox));
textBox.SetValue(NameProperty, "exemplaryName");
textBox.SetValue(HorizontalAlignmentProperty, HorizontalAlignment.Stretch);
FrameworkElementFactory textBlock = new FrameworkElementFactory(typeof(TextBlock));
textBlock.SetValue(TextBlock.TextProperty, "exemplaryName");
FrameworkElementFactory stackPanel = new FrameworkElementFactory(typeof(StackPanel));
stackPanel.AppendChild(textBox);
stackPanel.AppendChild(textBlock);
DataTemplate headerTemplate = new DataTemplate();
headerTemplate.VisualTree = stackPanel;
column.HeaderTemplate = headerTemplate;
temp.dataGrid.Columns.Add(column);
Recent view of DataGrid column headers:
What is my problem. When I am setting TextBox HorizontalAlignment value to "Stretch" nothing happened. As you can see in the picture above columns have some specific width (which value is equal to Auto) but TextBox HorizontalAlignment remains with its default value.
My goal is to have this TextBoxes stretched even when user will resize a column (dragging left or right).
How to set TextBox value HorizontalAlignment to stretch? Where I am missing something? Does any additional binding or some other mechanism is necessary to obtain stretching when width of the column is changing?
c# wpf wpf-controls code-behind
New contributor
I am trying to write simple multi-filtering for datagrid. The concept is to add TextBox to each column header in data grid. User may type some value in each TextBox and then rows in DataGrid will be filtered.
Columns are generated dynamiclly. I am doing this in code-behind.
To add the TextBox to column header I have created new DataTemplate and assigned that to HeaderTemplate of column as below:
DataGridTextColumn column = new DataGridTextColumn();
FrameworkElementFactory textBox = new FrameworkElementFactory(typeof(TextBox));
textBox.SetValue(NameProperty, "exemplaryName");
textBox.SetValue(HorizontalAlignmentProperty, HorizontalAlignment.Stretch);
FrameworkElementFactory textBlock = new FrameworkElementFactory(typeof(TextBlock));
textBlock.SetValue(TextBlock.TextProperty, "exemplaryName");
FrameworkElementFactory stackPanel = new FrameworkElementFactory(typeof(StackPanel));
stackPanel.AppendChild(textBox);
stackPanel.AppendChild(textBlock);
DataTemplate headerTemplate = new DataTemplate();
headerTemplate.VisualTree = stackPanel;
column.HeaderTemplate = headerTemplate;
temp.dataGrid.Columns.Add(column);
Recent view of DataGrid column headers:
What is my problem. When I am setting TextBox HorizontalAlignment value to "Stretch" nothing happened. As you can see in the picture above columns have some specific width (which value is equal to Auto) but TextBox HorizontalAlignment remains with its default value.
My goal is to have this TextBoxes stretched even when user will resize a column (dragging left or right).
How to set TextBox value HorizontalAlignment to stretch? Where I am missing something? Does any additional binding or some other mechanism is necessary to obtain stretching when width of the column is changing?
c# wpf wpf-controls code-behind
c# wpf wpf-controls code-behind
New contributor
New contributor
edited 2 days ago
Armali
6,76793595
6,76793595
New contributor
asked 2 days ago
martinez
133
133
New contributor
New contributor
It looks like your text box ends up in another container that has exactly that size. Stretch doesn't mean "Push" but rather "occupy what you can". If you snoop this you will find that it does indeed stretch.
– Rob
2 days ago
add a comment |
It looks like your text box ends up in another container that has exactly that size. Stretch doesn't mean "Push" but rather "occupy what you can". If you snoop this you will find that it does indeed stretch.
– Rob
2 days ago
It looks like your text box ends up in another container that has exactly that size. Stretch doesn't mean "Push" but rather "occupy what you can". If you snoop this you will find that it does indeed stretch.
– Rob
2 days ago
It looks like your text box ends up in another container that has exactly that size. Stretch doesn't mean "Push" but rather "occupy what you can". If you snoop this you will find that it does indeed stretch.
– Rob
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
Use a ColumnHeaderStyle
that sets the HorizontalContentAlignment
to true
:
<DataGrid x:Name="dataGrid">
<DataGrid.ColumnHeaderStyle>
<Style TargetType="x:Type DataGridColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</DataGrid.ColumnHeaderStyle>
</DataGrid>
You could create the Style
in the code-behind if you have to:
const string Xaml = "<Style xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" TargetType="x:Type DataGridColumnHeader"><Setter Property="HorizontalContentAlignment" Value="Stretch" /></Style>";
temp.dataGrid.ColumnHeaderStyle = XamlReader.Parse(Xaml) as Style;
Wow! It works perfectly as I expected.That is what I wanted to achieve. Thank you very much!
– martinez
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Use a ColumnHeaderStyle
that sets the HorizontalContentAlignment
to true
:
<DataGrid x:Name="dataGrid">
<DataGrid.ColumnHeaderStyle>
<Style TargetType="x:Type DataGridColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</DataGrid.ColumnHeaderStyle>
</DataGrid>
You could create the Style
in the code-behind if you have to:
const string Xaml = "<Style xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" TargetType="x:Type DataGridColumnHeader"><Setter Property="HorizontalContentAlignment" Value="Stretch" /></Style>";
temp.dataGrid.ColumnHeaderStyle = XamlReader.Parse(Xaml) as Style;
Wow! It works perfectly as I expected.That is what I wanted to achieve. Thank you very much!
– martinez
2 days ago
add a comment |
up vote
0
down vote
accepted
Use a ColumnHeaderStyle
that sets the HorizontalContentAlignment
to true
:
<DataGrid x:Name="dataGrid">
<DataGrid.ColumnHeaderStyle>
<Style TargetType="x:Type DataGridColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</DataGrid.ColumnHeaderStyle>
</DataGrid>
You could create the Style
in the code-behind if you have to:
const string Xaml = "<Style xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" TargetType="x:Type DataGridColumnHeader"><Setter Property="HorizontalContentAlignment" Value="Stretch" /></Style>";
temp.dataGrid.ColumnHeaderStyle = XamlReader.Parse(Xaml) as Style;
Wow! It works perfectly as I expected.That is what I wanted to achieve. Thank you very much!
– martinez
2 days ago
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Use a ColumnHeaderStyle
that sets the HorizontalContentAlignment
to true
:
<DataGrid x:Name="dataGrid">
<DataGrid.ColumnHeaderStyle>
<Style TargetType="x:Type DataGridColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</DataGrid.ColumnHeaderStyle>
</DataGrid>
You could create the Style
in the code-behind if you have to:
const string Xaml = "<Style xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" TargetType="x:Type DataGridColumnHeader"><Setter Property="HorizontalContentAlignment" Value="Stretch" /></Style>";
temp.dataGrid.ColumnHeaderStyle = XamlReader.Parse(Xaml) as Style;
Use a ColumnHeaderStyle
that sets the HorizontalContentAlignment
to true
:
<DataGrid x:Name="dataGrid">
<DataGrid.ColumnHeaderStyle>
<Style TargetType="x:Type DataGridColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</DataGrid.ColumnHeaderStyle>
</DataGrid>
You could create the Style
in the code-behind if you have to:
const string Xaml = "<Style xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" TargetType="x:Type DataGridColumnHeader"><Setter Property="HorizontalContentAlignment" Value="Stretch" /></Style>";
temp.dataGrid.ColumnHeaderStyle = XamlReader.Parse(Xaml) as Style;
edited 2 days ago
answered 2 days ago
mm8
78.8k81731
78.8k81731
Wow! It works perfectly as I expected.That is what I wanted to achieve. Thank you very much!
– martinez
2 days ago
add a comment |
Wow! It works perfectly as I expected.That is what I wanted to achieve. Thank you very much!
– martinez
2 days ago
Wow! It works perfectly as I expected.That is what I wanted to achieve. Thank you very much!
– martinez
2 days ago
Wow! It works perfectly as I expected.That is what I wanted to achieve. Thank you very much!
– martinez
2 days ago
add a comment |
martinez is a new contributor. Be nice, and check out our Code of Conduct.
martinez is a new contributor. Be nice, and check out our Code of Conduct.
martinez is a new contributor. Be nice, and check out our Code of Conduct.
martinez is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53225209%2fhow-to-set-stretching-for-textbox-in-column-header-in-code-behind%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
It looks like your text box ends up in another container that has exactly that size. Stretch doesn't mean "Push" but rather "occupy what you can". If you snoop this you will find that it does indeed stretch.
– Rob
2 days ago