C# 代码隐藏中未识别XAML控件

C# 代码隐藏中未识别XAML控件,c#,xaml,microsoft-metro,winrt-xaml,C#,Xaml,Microsoft Metro,Winrt Xaml,我有一个奇怪的问题,XAML控件在代码隐藏中不可见。以下是XAML的一个示例: <ListView Name="lvtest" Grid.Row="2" Grid.ColumnSpan="2" Margin="0,20,0,0" ItemsSource="{Binding Content}" > <ListView.ItemTemplate> <DataTemplate>

我有一个奇怪的问题,XAML控件在代码隐藏中不可见。以下是XAML的一个示例:

<ListView Name="lvtest" Grid.Row="2" Grid.ColumnSpan="2" Margin="0,20,0,0" 
              ItemsSource="{Binding Content}" >
    <ListView.ItemTemplate>
        <DataTemplate>     
            <StackPanel>                                
                <WebView Name="contentView" Style="{StaticResource BodyTextStyle}" />
                <TextBlock Name="testtxt" Text="{Binding}" Style="{StaticResource BodyTextStyle}" Foreground="GreenYellow"/>

在代码隐藏中:

  private void FindElement(object sender, RoutedEventArgs e)

    {

        // get the current selected item

        ListViewItem item = listview.ItemContainerGenerator.ContainerFromIndex(listview.SelectedIndex) as ListViewItem;

        TextBlock textYear = null;

        if (item != null)

        {

            //get the item's template parent

            ContentPresenter templateParent = GetFrameworkElementByName<ContentPresenter>(item);

            //get the DataTemplate that TextBlock in.

            DataTemplate dataTemplate = listview.ItemTemplate;

            if (dataTemplate != null && templateParent != null)

            {

                 textYear = dataTemplate.FindName("textYear", templateParent) as TextBlock;

            }

            if (textYear != null)

            {

                MessageBox.Show(String.Format("Current item's Year is:{0}", textYear.Text));

            }

        }



    }

    private static T GetFrameworkElementByName<T>(FrameworkElement referenceElement) where T : FrameworkElement

    {

        FrameworkElement child = null;

        for (Int32 i = 0; i < VisualTreeHelper.GetChildrenCount(referenceElement); i++)

        {

            child = VisualTreeHelper.GetChild(referenceElement, i) as FrameworkElement;

            System.Diagnostics.Debug.WriteLine(child);

            if (child != null && child.GetType() == typeof(T))

            { break; }

            else if (child != null)

            {

                child = GetFrameworkElementByName<T>(child);

                if (child != null && child.GetType() == typeof(T))

                {

                    break;

                }

            }

        }

        return child as T;

    }
此.lvtest可识别,但:

this.contentView
this.testtxt
都不是

我还尝试了
x:Name

很明显,我遗漏了一些明显的东西,我只是看不出是什么

编辑:


为了澄清,textbox控件将用于显示一些基于绑定的格式化文本,但我发现文本是HTML格式的(建议使用WebView控件)。据我所知,我需要
NavigateToString
来使用WebView控件,因此无法将其本身绑定。

它们是隐藏的,因为它们位于模板控件内


1/2/2007
1/3/2008
1/5/2007
1/6/2006
在代码隐藏中:

  private void FindElement(object sender, RoutedEventArgs e)

    {

        // get the current selected item

        ListViewItem item = listview.ItemContainerGenerator.ContainerFromIndex(listview.SelectedIndex) as ListViewItem;

        TextBlock textYear = null;

        if (item != null)

        {

            //get the item's template parent

            ContentPresenter templateParent = GetFrameworkElementByName<ContentPresenter>(item);

            //get the DataTemplate that TextBlock in.

            DataTemplate dataTemplate = listview.ItemTemplate;

            if (dataTemplate != null && templateParent != null)

            {

                 textYear = dataTemplate.FindName("textYear", templateParent) as TextBlock;

            }

            if (textYear != null)

            {

                MessageBox.Show(String.Format("Current item's Year is:{0}", textYear.Text));

            }

        }



    }

    private static T GetFrameworkElementByName<T>(FrameworkElement referenceElement) where T : FrameworkElement

    {

        FrameworkElement child = null;

        for (Int32 i = 0; i < VisualTreeHelper.GetChildrenCount(referenceElement); i++)

        {

            child = VisualTreeHelper.GetChild(referenceElement, i) as FrameworkElement;

            System.Diagnostics.Debug.WriteLine(child);

            if (child != null && child.GetType() == typeof(T))

            { break; }

            else if (child != null)

            {

                child = GetFrameworkElementByName<T>(child);

                if (child != null && child.GetType() == typeof(T))

                {

                    break;

                }

            }

        }

        return child as T;

    }
private void FindElement(对象发送方、路由目标方)
{
//获取当前选定的项目
ListViewItem项=listview.ItemContainerGenerator.ContainerFromIndex(listview.SelectedIndex)作为ListViewItem;
TextBlock textYear=null;
如果(项!=null)
{
//获取项目的模板父级
ContentPresenter templateParent=GetFrameworkElementByName(项);
//获取文本块所在的数据模板。
DataTemplate DataTemplate=listview.ItemTemplate;
if(dataTemplate!=null&&templateParent!=null)
{
textYear=dataTemplate.FindName(“textYear”,templateParent)作为TextBlock;
}
如果(textYear!=null)
{
Show(String.Format(“当前项目的年份为:{0}”,textYear.Text));
}
}
}
私有静态T GetFrameworkElementByName(FrameworkElement引用元素),其中T:FrameworkElement
{
FrameworkElement子元素=null;
for(Int32 i=0;i
您缺少的是,数据模板中定义的元素可能在页面上出现任意次数,甚至在运行时可能会发生更改。哪一个应该链接到
contentView
字段?没有很好的方法来回答这个问题,因此它不会创建
contentView
字段

换句话说,您并不是定义页面包含这些元素,而是声明了一个.NET可以从中创建元素的模板

如果你让我们知道你是如何使用它的,我们可能会建议另一种方法

编辑:类似的内容可能适合您:

<WebView Loaded="contentView_Loaded" Style="{StaticResource BodyTextStyle}" />

我已经编辑了这个问题。基本上,我试图在一个绑定列表中显示格式化的HTML(这就是TextBlock最初的用途)。这不起作用,但你让我走上了正确的轨道-谢谢
void contentView_Loaded(object sender, EventArgs e)
{
    var contentView = (WebView)sender;
    var dataContext = (YourDataType)contentView.DataContext;
    // do something
}