如何更改每个 DataGrid 行详细信息的行详细信息的 DataTemplate 中的 TextBlock 的文本?
我有 Datagrid,通过鼠标在每一行中单击显示数据网格行详细信息。这是代码,
Microsoft.Windows.Controls.DataGridRow row = (Microsoft.Windows.Controls.DataGridRow)(DataGrid1.ItemContainerGenerator.ContainerFromItem(DataGrid1.SelectedItem));
DataGridDetailsPresenter presenter = FindVisualChild<DataGridDetailsPresenter>(row);
DataTemplate template = presenter.ContentTemplate;
TextBlock txt = (TextBlock)template.FindName("rowdetails", presenter);
txt.Text = retString;
而且我还有复选框,当您选中它时,它应该显示所有行详细信息。 我正在尝试使用此代码来显示所有行详细信息,
if ((bool)chkboxRowDetails.IsChecked)
{
DataGrid1.RowDetailsVisibilityMode = Microsoft.Windows.Controls.DataGridRowDetailsVisibilityMode.Visible;
for (int i = 0; i < DataGrid1.Items.Count-1; i++)
{
Microsoft.Windows.Controls.DataGridRow row = (Microsoft.Windows.Controls.DataGridRow)(DataGrid1.ItemContainerGenerator.ContainerFromIndex(i));
DataGridDetailsPresenter presenter = FindVisualChild<DataGridDetailsPresenter>(row);
DataTemplate template =presenter.ContentTemplate;
TextBlock txt = (TextBlock)template.FindName("rowdetails", presenter);
txt.Text = retString;
}
但它给出了错误。 “此操作仅对应用了此模板的元素有效。” 在行中显示 TextBlock txt = (TextBlock)template.FindName("rowdetails", Presenter); 你知道我的代码有什么问题吗?我想通过选中复选框来显示所有行详细信息。我的数据模板在这里
<WpfToolkit:DataGrid.RowDetailsTemplate>
<DataTemplate>
<StackPanel HorizontalAlignment="Stretch" Orientation="Vertical" Margin="5">
<TextBlock Foreground="CadetBlue" FontSize="14"
TextWrapping="Wrap" Name="rowdetails" HorizontalAlignment="Stretch"
/>
</StackPanel>
</DataTemplate>
</WpfToolkit:DataGrid.RowDetailsTemplate>
I have Datagrid which is clicking by mouse in each row is showing data grid row details. here is code,
Microsoft.Windows.Controls.DataGridRow row = (Microsoft.Windows.Controls.DataGridRow)(DataGrid1.ItemContainerGenerator.ContainerFromItem(DataGrid1.SelectedItem));
DataGridDetailsPresenter presenter = FindVisualChild<DataGridDetailsPresenter>(row);
DataTemplate template = presenter.ContentTemplate;
TextBlock txt = (TextBlock)template.FindName("rowdetails", presenter);
txt.Text = retString;
And also I have Checkbox, when you check it, it should show all row details.
I am trying this code for showing all rowdetails
if ((bool)chkboxRowDetails.IsChecked)
{
DataGrid1.RowDetailsVisibilityMode = Microsoft.Windows.Controls.DataGridRowDetailsVisibilityMode.Visible;
for (int i = 0; i < DataGrid1.Items.Count-1; i++)
{
Microsoft.Windows.Controls.DataGridRow row = (Microsoft.Windows.Controls.DataGridRow)(DataGrid1.ItemContainerGenerator.ContainerFromIndex(i));
DataGridDetailsPresenter presenter = FindVisualChild<DataGridDetailsPresenter>(row);
DataTemplate template =presenter.ContentTemplate;
TextBlock txt = (TextBlock)template.FindName("rowdetails", presenter);
txt.Text = retString;
}
But it is giving error. "This operation is valid only on elements that have this template applied."
Showing in line TextBlock txt = (TextBlock)template.FindName("rowdetails", presenter);
Do you have any idea what is wrong in my code. I want to show all row details by checking checkbox. My Data template is here
<WpfToolkit:DataGrid.RowDetailsTemplate>
<DataTemplate>
<StackPanel HorizontalAlignment="Stretch" Orientation="Vertical" Margin="5">
<TextBlock Foreground="CadetBlue" FontSize="14"
TextWrapping="Wrap" Name="rowdetails" HorizontalAlignment="Stretch"
/>
</StackPanel>
</DataTemplate>
</WpfToolkit:DataGrid.RowDetailsTemplate>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过在行详细信息加载中添加一些代码解决了这个问题。这是代码。
I solved this problem by adding some codes in row details load. Here is code.