如何更改每个DataGrid行详细信息的行详细信息数据模板中的TextBlock文本? [英] How to change Text of TextBlock which is in DataTemplate of Row Details for each DataGrid Row Details?

查看:92
本文介绍了如何更改每个DataGrid行详细信息的行详细信息数据模板中的TextBlock文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Datagrid,它在每一行中用鼠标单击显示数据网格行的详细信息。这是代码

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;

我还有Checkbox,当您选中它时,它应该显示所有行的详细信息。
我正在尝试这段代码以显示所有rowdetails

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;


                }

但是它给出了错误。 此操作仅对应用了此模板的元素有效。
在线显示TextBlock txt =(TextBlock)template.FindName( rowdetails,演示者);
您知道我的代码有什么问题吗?我想通过选中复选框显示所有行的详细信息。我的数据模板在这里

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>


推荐答案

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

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. 

这篇关于如何更改每个DataGrid行详细信息的行详细信息数据模板中的TextBlock文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆