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

时间:2012-02-16 12:41:49

标签: wpf wpfdatagrid wpftoolkit

我有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;

我也有Checkbox,当你检查它时,它应该显示所有行详细信息。 我正在尝试使用此代码显示所有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”,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>

1 个答案:

答案 0 :(得分:1)

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

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