[英]Dynamic Column headers and Header count in Datagrid WPF
我正在WPF應用程序中構造一個數據網格。 Datagrid的ItemSource
綁定到IEnumerable
集合。 我沒有我的項目的ViewModel。 當我將datagrid itemsource綁定到datagrid時,我會即時獲得列標題和Row值。
我不知道標題。 可能是任何東西。 我需要在網格的datagrid中顯示所選行的詳細信息。
為此,我需要將SelectedItem.HeaderName綁定到網格的文本塊。 但是,這里的問題是我沒有標題的名稱。 所以我不能簡單地對SelectedItem.Headername
硬編碼。
並且列數可以分別不同。 因此,當我的一行數據網格被選中時,我的詳細視圖還應該動態編號標頭名稱及其相應的值。
截至目前,我已經編碼並在xaml中看到了結果,如下所示。 因為對於特定文件,我知道它們各自的列標題,
<Label HorizontalAlignment="Stretch"
VerticalAlignment="Center"
Grid.Row="0"
Grid.Column="0">Header2:
</Label>
<TextBlock Grid.Row="0"
Grid.Column="1"
Name="Header2"
Text="{Binding SelectedItem.date, ElementName=dataGrid1}"
Width="auto"
Height="auto"
HorizontalAlignment="Stretch"
VerticalAlignment="Center" />
<Label Grid.Row="0"
Grid.Column="2"
VerticalAlignment="Center">Header3:</Label>
<TextBlock Grid.Row="0"
Grid.Column="3"
Name="username"
Text="{Binding SelectedItem.Header3, ElementName=dataGrid1}"
Width="auto"
Height="auto"
HorizontalAlignment="Stretch"
VerticalAlignment="Center" />
<Label Grid.Row="0"
Grid.Column="4"
VerticalAlignment="Center">Header4:</Label>
<TextBlock Grid.Row="0"
Grid.Column="5"
Name="level"
Text="{Binding SelectedItem.header4, ElementName=dataGrid1}"
Width="auto"
Height="auto"
HorizontalAlignment="Stretch"
VerticalAlignment="Center" />
<Label Grid.Row="1"
Grid.Column="0"
VerticalAlignment="Center">Header5:</Label>
<TextBlock Grid.Row="1"
Grid.Column="1"
Name="logger"
Text="{Binding SelectedItem.header5, ElementName=dataGrid1}"
Width="auto"
Height="auto"
HorizontalAlignment="Stretch"
VerticalAlignment="Center" />
<Label Grid.Row="1"
Grid.Column="2"
VerticalAlignment="Center">Headr6:</Label>
<TextBlock Grid.Row="1"
Grid.Column="3"
Name="thread"
Text="{Binding SelectedItem.header6, ElementName=dataGrid1}"
Width="auto"
Height="auto"
HorizontalAlignment="Stretch"
VerticalAlignment="Center" />
由於我是一個初學者,所以我不知道該怎么做。 如果您嘗試幫助我,我會很高興。 並提出一些我需要閱讀的與此動態列生成有關的概念,即Count,將動態列標題分配給UI中的其他控件。 提前致謝!
我做了一個小項目,其中包含動態生成的網格(TextBlock + TextBox Clollection),具體取決於在DataGrid中進行收集的DataGrid標頭和對象屬性。 希望這就是您想要的。 您可以從我的SkyDrive中下載它
XAML:
<Grid>
<StackPanel>
<StackPanel x:Name="myStackPanel" Orientation="Vertical"></StackPanel>
<DataGrid x:Name="myDataGrid" ItemsSource="{Binding MySource}" AutoGenerateColumns="True">
</DataGrid>
</StackPanel>
</Grid>
我已經在Loaded事件處理程序中設置了它:
for (int i = 0; i < myDataGrid.Columns.Count; i++)
{
var childStackPanel = new StackPanel { Orientation = Orientation.Horizontal };
var myTextBlock = new TextBlock { Text = myDataGrid.Columns[i].Header + " : " };
var myTextBox = new TextBox { Width = 200 };
Type myType = typeof(Text);
IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());
myTextBox.SetBinding(TextBox.TextProperty,
new Binding("SelectedItem." + props[i].Name) { ElementName = "myDataGrid" });
childStackPanel.Children.Add(myTextBlock);
childStackPanel.Children.Add(myTextBox);
myStackPanel.Children.Add(childStackPanel);
}
文字課:
public class TranslationText
{
private string _translation;
private bool _isTranslated;
public string Translation
{
get { return _translation; }
set
{
_translation = value;
}
}
public bool IsTranslated
{
get { return _isTranslated; }
set
{
_isTranslated = value;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.