如何更改每个 DataGrid 行详细信息的行详细信息的 DataTemplate 中的 TextBlock 的文本?

发布于 2025-01-06 18:40:42 字数 2039 浏览 0 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿挽旧人 2025-01-13 18:40:42

我通过在行详细信息加载中添加一些代码解决了这个问题。这是代码。

        TextBlock txt1 = e.DetailsElement.FindName("rowdetails") as TextBlock;
        txt1.Text = retString; // where retString is variable string. 

I solved this problem by adding some codes in row details load. Here is code.

        TextBlock txt1 = e.DetailsElement.FindName("rowdetails") as TextBlock;
        txt1.Text = retString; // where retString is variable string. 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文