以编程方式在Silverlight DataGrid详细信息中设置内容 [英] Programmatically set content in Silverlight DataGrid details

查看:69
本文介绍了以编程方式在Silverlight DataGrid详细信息中设置内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据外部设置文件中的信息动态设置DataGrid模板中的内容。该设置文件指定应在DataGrid中显示哪些数据字段。应用程序的管理员可以编辑设置以更改要显示的字段。我无法对要显示的字段进行硬编码。

I need to dynamically set the contents within the template of a DataGrid based on information in an external settings file. That settings file specifies which data fields should display in the DataGrid. The administrator of the application can edit the settings to change the fields to display. I cannot hard-code the fields to display.

我可以在运行时轻松地将列(DataGridTextColumn的)添加到DataGrid中。我根据设置在项目源中的字段上设置了绑定,并且显示良好。

I can easily add the columns (DataGridTextColumn's) to the DataGrid at runtime. I set a binding to a field in the item source based on the settings, and that displays fine.

现在,当用户单击一行时,我需要显示详细信息。我使用DataTemplate设置了RowDetailsTemplate,并在其中添加了Grid(或StackPanel)以格式化细节。如果我将带有字段绑定的TextBlocks添加到标记中,它将显示详细信息。

Now I need to display details when the user clicks a row. I set up a RowDetailsTemplate with DataTemplate, and added a Grid (or a StackPanel) inside to format the details. If I add to the markup the TextBlocks with bindings to fields, it displays the details just fine.

但是如何在详细信息中设置Grid / StackPanel的内容以编程方式模板?如果我在启动时尝试按名称引用Grid / StackPanel控件,则它们为null(例如,在页面Loaded事件中)。我尝试使用Grid / StackPanel上的Loaded事件添加详细信息。该代码将运行并显示将内容添加到Grid / StackPanel,但是当我单击该行时实际上什么也没有显示。我猜测问题是模板/网格已经加载,并且忽略了我所做的更改。

But how can I set the content of the Grid/StackPanel in the details template programmatically? The Grid/StackPanel controls are null if I try to reference them by name on startup (e.g., in the page Loaded event). I have tried using the Loaded event on the Grid/StackPanel to add the details. That code runs and appears to add the content to the Grid/StackPanel, but nothing actually appears when I click the row. I'm guessing that the problem is that the template/Grid is already loaded and ignores the changes I'm making.

这里是我正在使用的代码示例在Loaded事件的处理程序中。即使我做这样简单的事情,单击行时也不会显示详细信息窗格。

Here's a sample of the code I'm using in the handler for the Loaded event. Even if I do something as simple as this, the details pane doesn't appear when clicking on the row.

<data:DataGrid.RowDetailsTemplate>
    <DataTemplate>
        <Border Background="LightBlue" >
            <StackPanel x:Name="resultsDetailsPanel"
                Orientation="Vertical"
                Loaded="resultsDetailsPanel_Loaded">
            </StackPanel>
        </Border>
    </DataTemplate>
</data:DataGrid.RowDetailsTemplate>



    private void resultsDetailsPanel_Loaded(object sender, RoutedEventArgs e)
    {
        if (_resultsGridLoaded)
            return;

        StackPanel detailsPanel = sender as StackPanel;

        TextBlock fieldNameTextBlock = new TextBlock();
        fieldNameTextBlock.Text = "TESTING";
        detailsPanel.Children.Add(fieldNameTextBlock);

        _resultsGridLoaded = true;
    }


推荐答案

我实际上尝试了您的代码,工作中。您应该检查两件事:

I actually tried your code and is working. Two things you should check:


  • 您的_resultsGridLoaded变量是否初始化为false?

  • 您设置了吗您的DataGrid上是否有RowDetailsVisibilityMode = VisibleWhenSelected?

更新:由于某种原因,它不再起作用。但是我确实找到了两种解决方法:

UPDATE: For some reason is not working anymore. But I did found two ways you can fix it:


  1. 删除resultsGridLoaded逻辑。

  2. 如果您需要这种逻辑,可以在DataGrid上为SelectionChanged事件添加一个处理程序,在其中可以将_resultsGridLoaded变量设置为false,以便新的StackPanel可以正确添加其内容:





    
        
            
                
                
            
        
    


后面的代码:

private void resultsPanel_Loaded(object sender, RoutedEventArgs e)
{
    if (_resultsGridLoaded)
        return;

    StackPanel pane = (StackPanel)sender;

    TextBlock newChild = new TextBlock()
    {
        Text = "New text"
    };

    pane.Children.Add(newChild);

    _resultsGridLoaded = true;
}

private void grid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    _resultsGridLoaded = false;
}

希望这会有所帮助

这篇关于以编程方式在Silverlight DataGrid详细信息中设置内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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