从wpf中的数据模板检索内容属性

发布于 2024-10-09 06:42:01 字数 2219 浏览 2 评论 0原文

我有一个包含不同文本块的列表框项目的特定模板。在列表框中添加项目后,我想检索特定列表框项目的特定文本块的文本。我怎样才能做到这一点?

 <ListBox x:Name="listBox1" Grid.Row="0" SelectionChanged="listBox1_SelectionChanged">             
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal"  Margin="0,2,0,0">
                        <CheckBox Name="cbSelect" VerticalAlignment="Center" Visibility="{Binding isVisible}"/>                                                            
                        <StackPanel Orientation="Vertical">
                            <Grid HorizontalAlignment="Stretch" >
                                <TextBlock Name="txtTitle" VerticalAlignment="Bottom"  Text="{Binding Title}"  Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                                <TextBlock Name="txtDate" Foreground="{StaticResource PhoneInverseInactiveBrush}"  HorizontalAlignment="Right" VerticalAlignment="Center" Text="{Binding CreatedOn}" Margin="0,0,30,0" Style="{StaticResource PhoneTextSmallStyle}" />
                          <!--  <Image x:Name="lineImg" Source="/Icons/appbar.next.rest.png" HorizontalAlignment="Right"  VerticalAlignment="Bottom" Width="48" Height="48"/>-->
                            </Grid>
                            <TextBlock Name="txtContent"  Foreground="{StaticResource PhoneDisabledBrush}" VerticalAlignment="Bottom" Text="{Binding Content}" Style="{StaticResource PhoneTextNormalStyle}" />                            
                            <Line Stroke="{StaticResource PhoneBorderBrush}"  StrokeThickness=" 2" X1="0" Y1="0" X2="{Binding ElementName=listBox1, Path=ActualWidth}" Y2="0" Margin="0,6,0,0" ></Line>
                        <!--   <Image x:Name="lineImg" Source="/Icons/Line.png" HorizontalAlignment="Center" Width="500" Height="2" Margin="0,15,0,0" />-->
                        </StackPanel>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

在上面的代码中,将项目添加到列表框后,我想要特定项目的 txtTitle 文本。我怎样才能得到它?

I have a particular template for a listbox item which contains different textblocks. After adding the items in the listbox I want to retrieve the text of a particular textblock of a particular listbox item. How can I do that?

 <ListBox x:Name="listBox1" Grid.Row="0" SelectionChanged="listBox1_SelectionChanged">             
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal"  Margin="0,2,0,0">
                        <CheckBox Name="cbSelect" VerticalAlignment="Center" Visibility="{Binding isVisible}"/>                                                            
                        <StackPanel Orientation="Vertical">
                            <Grid HorizontalAlignment="Stretch" >
                                <TextBlock Name="txtTitle" VerticalAlignment="Bottom"  Text="{Binding Title}"  Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                                <TextBlock Name="txtDate" Foreground="{StaticResource PhoneInverseInactiveBrush}"  HorizontalAlignment="Right" VerticalAlignment="Center" Text="{Binding CreatedOn}" Margin="0,0,30,0" Style="{StaticResource PhoneTextSmallStyle}" />
                          <!--  <Image x:Name="lineImg" Source="/Icons/appbar.next.rest.png" HorizontalAlignment="Right"  VerticalAlignment="Bottom" Width="48" Height="48"/>-->
                            </Grid>
                            <TextBlock Name="txtContent"  Foreground="{StaticResource PhoneDisabledBrush}" VerticalAlignment="Bottom" Text="{Binding Content}" Style="{StaticResource PhoneTextNormalStyle}" />                            
                            <Line Stroke="{StaticResource PhoneBorderBrush}"  StrokeThickness=" 2" X1="0" Y1="0" X2="{Binding ElementName=listBox1, Path=ActualWidth}" Y2="0" Margin="0,6,0,0" ></Line>
                        <!--   <Image x:Name="lineImg" Source="/Icons/Line.png" HorizontalAlignment="Center" Width="500" Height="2" Margin="0,15,0,0" />-->
                        </StackPanel>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

In the above code, after adding the items to the listbox, I want the text of txtTitle of a particular item. How can I get it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方的巷 2024-10-16 06:42:01

您需要获取对该项目的 ContentPresenter 的引用。

ListBoxItem myListBoxItem = (ListBoxItem)(myListBox.ItemContainerGenerator.ContainerFromItem(myListBox.Items.CurrentItem));
ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(myListBoxItem);
DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
TextBlock myTextBlock = (TextBlock)myDataTemplate.FindName("textBlock", myContentPresenter);

可以找到 FindVisualChild 方法以及完整示例此处

You need to get a reference to the ContentPresenter of the item.

ListBoxItem myListBoxItem = (ListBoxItem)(myListBox.ItemContainerGenerator.ContainerFromItem(myListBox.Items.CurrentItem));
ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(myListBoxItem);
DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
TextBlock myTextBlock = (TextBlock)myDataTemplate.FindName("textBlock", myContentPresenter);

The FindVisualChild method along with the full example can be found here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文