【问题标题】:XAML controls not recognised in code-behindXAML 控件在代码隐藏中无法识别
【发布时间】:2012-06-24 13:12:21
【问题描述】:

我有一个奇怪的问题,即 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.contentViewthis.testtxt 都不是。

我也试过x:Name

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

编辑:

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

【问题讨论】:

    标签: c# xaml microsoft-metro winrt-xaml


    【解决方案1】:

    您缺少的是 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
    }
    

    【讨论】:

    • 我已经编辑了这个问题。基本上我试图在绑定列表中显示格式化的 HTML(这就是 TextBlock 最初的用途)。
    • 那行不通,但你让我走上了正轨 - 谢谢
    【解决方案2】:

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

    来自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;
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-28
      • 1970-01-01
      • 1970-01-01
      • 2019-05-26
      相关资源
      最近更新 更多