3

我有一个奇怪的问题,即 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"/>

在后面的代码中:

this.lvtest被认可,但是:

this.contentView两者this.testtxt都不是。

我也试过了x:Name

我显然在这里遗漏了一些明显的东西,我只是看不到什么。

编辑:

澄清一下,文本框控件将用于显示一些基于绑定的格式化文本,但我发现文本是 HTML 格式的(建议使用 WebView 控件)。据我所知,我需要NavigateToString使用 WebView 控件,因此不能这样绑定它。

4

2 回答 2

8

您缺少的是 DataTemplate 中定义的元素可能会在页面上出现多次,甚至可以在运行时更改。哪一个应该与该contentView领域相关联?没有好的方法来回答这个问题,所以它根本不会创建一个contentView字段。

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

如果您让我们知道您尝试使用它的方式,我们或许可以建议另一种方式。

编辑:这样的事情可能对你有用:

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

然后在代码中:

void contentView_Loaded(object sender, EventArgs e)
{
    var contentView = (WebView)sender;
    var dataContext = (YourDataType)contentView.DataContext;
    // do something
}
于 2012-06-24T13:17:56.227 回答
3

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

来自http://social.msdn.microsoft.com/Forums/en/wpf/thread/29ecc8ee-26ee-4331-8f97-35ff9d3e6886

<ListView  Name="listview">

    <ListView.ItemTemplate>

        <DataTemplate>

            <StackPanel Orientation="Horizontal" >

            <TextBlock Name="textYear" Text="{Binding Year}" />

                <TextBlock  Text="  " />

             <TextBlock Name="textDayOffWeek" Text="{Binding DayOfWeek}" />

                </StackPanel>

        </DataTemplate>

    </ListView.ItemTemplate>

    <s:DateTime >1/2/2007</s:DateTime>

    <s:DateTime >1/3/2008</s:DateTime>

    <s:DateTime >1/5/2007</s:DateTime>

    <s:DateTime >1/6/2006</s:DateTime>

</ListView>

    <Button Width="180" Height="30" Content="Find TextBlock in DataTemplate" Click="FindElement" />

    </StackPanel>

在后面的代码中:

  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;

    }
于 2012-06-24T13:17:48.660 回答