将TabItem绑定到列表

时间:2014-09-04 09:01:46

标签: c# wpf xaml

基本概念是,当用户按下开始按钮并从工作线程返回新列表时,它将被添加到“列表列表”中,而不是在新创建的列表项中显示。 但是我似乎无法抓住新创建的tabitem上的数据网格.....仍然无法想象,FindName和任何尝试的方法

XAML

<TabControl x:Name="tabMain" Margin="8,63,10,10" IsSynchronizedWithCurrentItem="True" >
    <TabControl.ContentTemplate>
        <DataTemplate>
            <Grid x:Name="grMain" Background="#FFE5E5E5">
                <DataGrid x:Name="dgResults" IsReadOnly="True" AutoGenerateColumns="False" AlternatingRowBackground="#FFAAE8D5" ItemsSource="{Binding data}" >
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Key" Binding="{Binding col1}" Width="150">
                            <DataGridTextColumn.ElementStyle>
                                <Style>
                                    <Setter Property="TextBlock.TextWrapping" Value="Wrap" />
                                </Style>
                            </DataGridTextColumn.ElementStyle>
                        </DataGridTextColumn>
                        <DataGridTextColumn Header="Value" Binding="{Binding col2}" Width="*">
                            <DataGridTextColumn.ElementStyle>
                                <Style>
                                    <Setter Property="TextBlock.TextWrapping" Value="Wrap" />

                                </Style>
                            </DataGridTextColumn.ElementStyle>
                        </DataGridTextColumn>
                    </DataGrid.Columns>
                </DataGrid>

            </Grid>

        </DataTemplate>
    </TabControl.ContentTemplate>
</TabControl >
获取列表后

代码

lstFinal.Add(e.Result as List<Col1Col2>);
TabItem newtab = new TabItem();
newtab.DataContext = lstFinal[lstFinal.Count - 1];


newtab.Header = txtMaterial.Text;
tabMain.Items.Add(newtab);
tabMain.SelectedIndex = tabMain.Items.Count - 1;
DataGrid dg = newtab.FindName("dgResults") as DataGrid;
dg.ItemsSource = lstFinal[lstFinal.Count - 1];

1 个答案:

答案 0 :(得分:0)

您的问题是由于您尝试从DataTemplate内引用控件而引起的。在运行时,此模板可能已应用于大量控件,因此首先必须获取已应用模板的对象,然后然后您可以从{{1}访问控件}。

通过参考MSDN上的How to: Find DataTemplate-Generated Elements页面,您可以确切了解如何执行此操作。但是,简而言之,就是这样做(来自链接页面):

DataTemplate

// Getting the currently selected ListBoxItem // Note that the ListBox must have // IsSynchronizedWithCurrentItem set to True for this to work ListBoxItem myListBoxItem = (ListBoxItem)(myListBox.ItemContainerGenerator.ContainerFromItem(myListBox.Items.CurrentItem)); // Getting the ContentPresenter of myListBoxItem ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(myListBoxItem); // Finding textBlock from the DataTemplate that is set on that ContentPresenter DataTemplate myDataTemplate = myContentPresenter.ContentTemplate; TextBlock myTextBlock = (TextBlock)myDataTemplate.FindName("textBlock", myContentPresenter); // Do something to the DataTemplate-generated TextBlock MessageBox.Show("The text of the TextBlock of the selected list item: " + myTextBlock.Text); 方法的定义也可以在链接页面上找到,并附有对该过程的描述。