在GridView项中获取TextBox的内容

时间:2016-03-24 01:22:15

标签: c# wpf xaml win-universal-app

我想在GridView Item中获取TextBox的文本。

这是我的代码:

XAML:

<GridView x:Name="inventoryGridView" Margin="70,40,10,10" SelectionChanged="inventoryGridView_SelectionChanged">
        <GridView.ItemTemplate>
            <DataTemplate>
                <StackPanel Margin="0,0,0,0"  Width="121" Height="128" VerticalAlignment="Top" Background="#19000000">
                    <TextBox Text="{Binding ItemId}" Name="InvItemId"/> 

                    ...

                </StackPanel>
        </GridView.ItemTemplate>
</GridView>

我该怎么做?

3 个答案:

答案 0 :(得分:1)

XAML:

<Window x:Class="WpfApplication1.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">
    <Window.Resources>
        <XmlDataProvider x:Key="data" XPath="/items/*">
            <x:XData>
                <items xmlns="">
                    <item value="Hello"/>
                    <item value="World" />
                </items>
            </x:XData>
        </XmlDataProvider>
    </Window.Resources>

    <StackPanel Orientation="Vertical">
        <ListView x:Name="MyListView" ItemsSource="{Binding Source={StaticResource data}, XPath=/items/item }">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Value">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Text="{Binding XPath=@value}" Name="GridTextBox"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
        <Button Click="Button_Click">Click Me</Button>
        <TextBlock x:Name="ResultTextBlock">Click the button</TextBlock>
    </StackPanel>
</Window>

代码背后:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        int theRowYouWant = 0;
        var listBoxItem = (ListBoxItem)MyListView.ItemContainerGenerator.ContainerFromIndex(theRowYouWant);
        var contentPresenter = FindVisualChild<ContentPresenter>(listBoxItem);
        var dataTemplate = contentPresenter.ContentTemplate;
        var txt = (TextBox)dataTemplate.FindName("GridTextBox", contentPresenter);
        ResultTextBlock.Text = txt.Text;
    }

    private childItem FindVisualChild<childItem>(DependencyObject obj)
        where childItem : DependencyObject
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(obj, i);
            if (child != null && child is childItem)
                return (childItem)child;
            else
            {
                childItem childOfChild = FindVisualChild<childItem>(child);
                if (childOfChild != null)
                    return childOfChild;
            }
        }
        return null;
    }
}

答案 1 :(得分:0)

要在网格列中显示文本框的文本,可以使用如下的绑定,

<TextBox Name="txtValue" />
<ListView>
    <ListView.View>
        <GridView>
            <GridViewColumn Header="TextBoxValue">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=Text, ElementName=txtValue}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

答案 2 :(得分:0)

我是这样做的:

DataSource

'items'是GridView的ItemsSource。