无法从TabControl DataTemplate获取控件 [英] Cant get a control from a TabControl DataTemplate

查看:111
本文介绍了无法从TabControl DataTemplate获取控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去2天,我一直在使用Google谷歌进行搜索,但是却一无所获,我只是无法对tabcontrol数据模板中的任何控件做任何事情。

I've been googling this for the last 2 days and cant get anywhere, I just cant do anything to any control in a datatemplate of a tabcontrol.

首先,代码:

private void Window_Loaded(object sender, RoutedEventArgs e) {
    tabControl1.ItemsSource = new string[] { "TabA", "TabB", "TabC" };
}

private void tabControl1_SelectionChanged(object sender, SelectionChangedEventArgs e) {
    ContentPresenter cp = tabControl1.Template.FindName("PART_SelectedContentHost", tabControl1) as ContentPresenter;

    DataTemplate dt = tabControl1.ContentTemplate;
    Grid g = tabControl1.ContentTemplate.FindName("myGrid", cp) as Grid;
    g.Background = new SolidColorBrush(Colors.Red);
}

xaml

<Window x:Class="tabTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
    <TabControl IsSynchronizedWithCurrentItem="True" Height="140" Name="tabControl1" Width="230" SelectionChanged="tabControl1_SelectionChanged">
        <TabControl.ContentTemplate>
            <DataTemplate>
                <Grid x:Name="myGrid">                        
                </Grid>
            </DataTemplate>    
        </TabControl.ContentTemplate>            
    </TabControl>
</Grid>

简而言之: / p>

In short this line:

Grid g = tabControl1.ContentTemplate.FindName("myGrid", cp) as Grid;

抛出错误 System.InvalidOperationException该操作仅对应用了此模板的元素有效。

throws an error "System.InvalidOperationException" This operation is valid only on elements that have this template applied.

我从这里

我发现了很多其他方式可以做到这一点,但我似乎无能为力:(希望有人可以为我指出正确的方向:)

I've found loads of other ways of doing this but I cant seem to get anywhere :( hope someone can point me in the right direction :)

推荐答案

运行方式实例化TabControl的方式似乎有问题时间。似乎第一次引发SelectionChanged事件时,ContentTemplate尚未完全可以访问。如果再次运行代码并跳过对ContentTemplate的首次访问,则会看到在随后的事件中可以访问此属性而不会引发异常。

Looks like it's issue with the way the TabControl is being instantiated by the run time. It appears that the first time the SelectionChanged event is being raised the ContentTemplate is not quite ready to be accessed. If you run your code again and skip over the first access of ContentTemplate you'll see that in subsequent events you can access this property without the exception being thrown.

通常可以通过调用Dispatcher.BeginInvoke来解决错误类型,在这种情况下,它允许运行时在执行代码之前完成对Tab控件的初始化。

Often these types of errors can be overcome by calling Dispatcher.BeginInvoke, in this case it allows the run time to finish initializing the tab control before executing your code.

Dispatcher.BeginInvoke(new Action(() =>
    {
        ContentPresenter cp = tabControl1.Template.FindName("PART_SelectedContentHost", tabControl1) as ContentPresenter;
        Grid g = tabControl1.ContentTemplate.FindName("myGrid", cp) as Grid;
        g.Background = new SolidColorBrush(Colors.Red);
    }));

这篇关于无法从TabControl DataTemplate获取控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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