如何将字符串变量绑定到绑定到DataSet的Datagrid行详细信息

时间:2022-03-27 14:18:46

I have Datagrid which is bound to my Database

我有Datagrid绑定到我的数据库

<WpfToolkit:DataGrid Name="DataGrid1" Grid.Row="1" 
      ItemsSource="{Binding Path=MainSearchBinding}" />

And I have row details which is bound to the Intro column of my table in database

我有行详细信息绑定到数据库中我的表的Intro列

<WpfToolkit:DataGrid.RowDetailsTemplate>            
    <DataTemplate>
        <StackPanel Orientation="Horizontal" Margin="5">
                <StackPanel Orientation="Vertical" Margin="5" >
                    <TextBlock Foreground="CadetBlue" FontSize="13" 
                    Width="Auto" TextWrapping="Wrap" 
                    Text="{Binding Path=Intro}"/>

            </StackPanel>
        </StackPanel>
    </DataTemplate>
</WpfToolkit:DataGrid.RowDetailsTemplate>

What I want is not to use Intro column in my table. I want to use string value from my code instead of column Intro. How can I bind string value to my row details? Thanks in advance.

我想要的是不要在我的表中使用Intro列。我想使用我的代码中的字符串值而不是列Intro。如何将字符串值绑定到我的行详细信息?提前致谢。

1 个解决方案

#1


0  

I modified my xaml code a little bit add Name to Text Block.

我修改了我的xaml代码,将Name添加到Text Block。

<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>

And the in code behind I add this code below to OnMouseClick. retString is my string variable. It is showing my string variable in each clicked row details.

在后面的代码我将下面的代码添加到OnMouseClick。 retString是我的字符串变量。它在每个单击的行详细信息中显示我的字符串变量。

 Microsoft.Windows.Controls.DataGridRow row = (Microsoft.Windows.Controls.DataGridRow)(DataGrid1.ItemContainerGenerator.ContainerFromItem(DataGrid1.SelectedItem));

            // Getting the ContentPresenter of the row details
            DataGridDetailsPresenter presenter = FindVisualChild<DataGridDetailsPresenter>(row);


            DataTemplate template = presenter.ContentTemplate;
            TextBlock txt = (TextBlock)template.FindName("rowdetails", presenter);
            txt.Text = retString;

Do not forget to add FindVisualChild in your code. Here is the code.

不要忘记在代码中添加FindVisualChild。这是代码。

private childItem FindVisualChild<childItem>(DependencyObject obj)
    where childItem : DependencyObject
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                if (child != null && child is childItem)
                    return (childItem)child;
                else
                {
                    childItem childOfChild = FindVisualChild<childItem>(child);
                    if (childOfChild != null)
                        return childOfChild;
                }
            }
            return null;
        }

I do not know why nobody answered to my question. Maybe it is not popular.

我不知道为什么没有人回答我的问题。也许它不受欢迎。

#1


0  

I modified my xaml code a little bit add Name to Text Block.

我修改了我的xaml代码,将Name添加到Text Block。

<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>

And the in code behind I add this code below to OnMouseClick. retString is my string variable. It is showing my string variable in each clicked row details.

在后面的代码我将下面的代码添加到OnMouseClick。 retString是我的字符串变量。它在每个单击的行详细信息中显示我的字符串变量。

 Microsoft.Windows.Controls.DataGridRow row = (Microsoft.Windows.Controls.DataGridRow)(DataGrid1.ItemContainerGenerator.ContainerFromItem(DataGrid1.SelectedItem));

            // Getting the ContentPresenter of the row details
            DataGridDetailsPresenter presenter = FindVisualChild<DataGridDetailsPresenter>(row);


            DataTemplate template = presenter.ContentTemplate;
            TextBlock txt = (TextBlock)template.FindName("rowdetails", presenter);
            txt.Text = retString;

Do not forget to add FindVisualChild in your code. Here is the code.

不要忘记在代码中添加FindVisualChild。这是代码。

private childItem FindVisualChild<childItem>(DependencyObject obj)
    where childItem : DependencyObject
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                if (child != null && child is childItem)
                    return (childItem)child;
                else
                {
                    childItem childOfChild = FindVisualChild<childItem>(child);
                    if (childOfChild != null)
                        return childOfChild;
                }
            }
            return null;
        }

I do not know why nobody answered to my question. Maybe it is not popular.

我不知道为什么没有人回答我的问题。也许它不受欢迎。