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?










share|improve this question









New contributor




martinez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • 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














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?










share|improve this question









New contributor




martinez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • 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












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?










share|improve this question









New contributor




martinez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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






share|improve this question









New contributor




martinez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




martinez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 days ago









Armali

6,76793595




6,76793595






New contributor




martinez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 days ago









martinez

133




133




New contributor




martinez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





martinez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






martinez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • 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




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












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;





share|improve this answer






















  • Wow! It works perfectly as I expected.That is what I wanted to achieve. Thank you very much!
    – martinez
    2 days ago










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);






martinez is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















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






























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;





share|improve this answer






















  • Wow! It works perfectly as I expected.That is what I wanted to achieve. Thank you very much!
    – martinez
    2 days ago














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;





share|improve this answer






















  • Wow! It works perfectly as I expected.That is what I wanted to achieve. Thank you very much!
    – martinez
    2 days ago












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;





share|improve this answer














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;






share|improve this answer














share|improve this answer



share|improve this answer








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
















  • 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










martinez is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















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.













 


draft saved


draft discarded














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














































































Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20