![](/img/trans.png)
[英]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.