2016-03-24 51 views
1

我想在GridView項目中獲取TextBox的文本。在GridView中獲取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> 

我怎麼能這樣做?

+0

你想從codeBehind獲取它嗎? –

+0

在'SelectionChanged'事件中,添加'var index = inventoryGridView.SelectedIndex; var a = Items [index] .ItemId;'在代碼中,Items是爲GridView的ItemsSource設置的集合。 –

回答

0

我做通過這種方式:

var a = items[inventoryGridView.SelectedIndex].ItemId; 

其中'items'是我的GridView的ItemsSource。

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> 
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 [email protected]}" 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; 
    } 
} 
+0

但我的代碼中沒有ListView。 – Redeil

相關問題