[英]How to get value of textbox in code-behind which was set by Javascript?
[英]How to set stretching for TextBox in column header in code-behind?
我正在尝试为datagrid编写简单的多重过滤。 概念是将TextBox添加到数据网格中的每个列标题。 用户可以在每个TextBox中键入一些值,然后将过滤DataGrid中的行。 列是动态生成的。 我在后台代码中执行此操作。 要将TextBox添加到列标题中,我创建了新的DataTemplate并将其分配给列的HeaderTemplate,如下所示:
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);
DataGrid列标题的最新视图:
我怎么了 当我将TextBox HorizontalAlignment值设置为“ Stretch”时,没有任何反应。 如上图所示,列具有特定的宽度(该值等于“自动”),但TextBox HorizontalAlignment保留其默认值。
我的目标是即使用户将调整列大小(向左或向右拖动)时,也可以拉伸此TextBoxes。
如何设置TextBox值HorizontalAlignment进行拉伸? 我在哪里想念什么? 更改列宽时,是否需要任何其他绑定或其他某种机制来获得拉伸?
使用ColumnHeaderStyle
,将HorizontalContentAlignment
设置为true
:
<DataGrid x:Name="dataGrid">
<DataGrid.ColumnHeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</DataGrid.ColumnHeaderStyle>
</DataGrid>
您可以在以下代码中创建Style
:
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.