none
如何操作gridview模板里的checkbox RRS feed

  • 问题

  • 我在gridview里面加入了checkbox控件如下

    <GridView x:Name="itemGridView"
                AutomationProperties.AutomationId="ItemsGridView"
                AutomationProperties.Name="minfo"
                ItemsSource="{Binding Source={StaticResource itemsViewSource}}"
                SelectionChanged="OnChange_SelectedMinfo" />
                <GridView.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="30"/>
                                <ColumnDefinition Width="*"/>                       
                            </Grid.ColumnDefinitions>
                            <CheckBox HorizontalAlignment="Center" CommandParameter="{Binding MId}"/>
                            <TextBlock Grid.Column="1" Text="{Binding Title}" Padding="5,7,0,0" Height="30" />
                      </Grid>
                    </DataTemplate>
                </GridView.ItemTemplate>
            </GridView>

    如何在后台设置checkbox为选中状态?如何得到checkbox是否为选中?


    2014年6月25日 2:21

答案

  • 你好,

    这个时候你应该使用VisualTreeHelper来帮助查找ItemTemplate里面的东西,查看一下教程吧: http://social.technet.microsoft.com/wiki/contents/articles/17502.finding-element-in-itemscontrol-datatemplate-listviewgridviewflipview-window-store-apps.aspx

    		/// <summary>
    		/// Extension method for a FrameworkElement that searches for a child element by type and name.
    		/// </summary>
    		/// <typeparam name="T">The type of the child element to search for.</typeparam>
    		/// <param name="element">The parent framework element.</param>
    		/// <param name="sChildName">The name of the child element to search for.</param>
    		/// <returns>The matching child element, or null if none found.</returns>
    		public static T FindElementByName<T>( this FrameworkElement element, string sChildName ) where T : FrameworkElement
    		{
    			Debug.WriteLine( "[FindElementByName] ==> element [{0}] sChildName [{1}] T [{2}]", element, sChildName, typeof( T ).ToString() );
    
    			T childElement = null;
    
    			//
    			// Spin through immediate children of the starting element.
    			//
    			var nChildCount = VisualTreeHelper.GetChildrenCount( element );
    			for ( int i = 0; i < nChildCount; i++ )
    			{
    				// Get next child element.
    				FrameworkElement child = VisualTreeHelper.GetChild( element, i ) as FrameworkElement;
    				Debug.WriteLine( "Found child [{0}]", child );
    
    				// Do we have a child?
    				if ( child == null )
    					continue;
    
    				// Is child of desired type and name?
    				if ( child is T && child.Name.Equals( sChildName ) )
    				{
    					// Bingo! We found a match.
    					childElement = (T) child;
    					Debug.WriteLine( "Found matching element [{0}]", childElement );
    					break;
    				} // if
    
    				// Recurse and search through this child's descendants.
    				childElement = FindElementByName<T>( child, sChildName );
    
    				// Did we find a matching child?
    				if ( childElement != null )
    					break;
    			} // for
    
    			Debug.WriteLine( "[FindElementByName] <== childElement [{0}]", childElement );
    			return childElement;
    		}

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2014年6月26日 11:16
    版主

全部回复


  •    index = (int)itemGridView.DataKeys[row.RowIndex].Value;
      bool result = ((CheckBox)row.cell[0].FindControl("CheckBox1")).Checked;

    试试吧

    2014年6月25日 4:03
  • C++里GridView没有DataKeys会员,而且row又是从哪里得来的变量?

    2014年6月25日 4:56
  • 你好,

    这个时候你应该使用VisualTreeHelper来帮助查找ItemTemplate里面的东西,查看一下教程吧: http://social.technet.microsoft.com/wiki/contents/articles/17502.finding-element-in-itemscontrol-datatemplate-listviewgridviewflipview-window-store-apps.aspx

    		/// <summary>
    		/// Extension method for a FrameworkElement that searches for a child element by type and name.
    		/// </summary>
    		/// <typeparam name="T">The type of the child element to search for.</typeparam>
    		/// <param name="element">The parent framework element.</param>
    		/// <param name="sChildName">The name of the child element to search for.</param>
    		/// <returns>The matching child element, or null if none found.</returns>
    		public static T FindElementByName<T>( this FrameworkElement element, string sChildName ) where T : FrameworkElement
    		{
    			Debug.WriteLine( "[FindElementByName] ==> element [{0}] sChildName [{1}] T [{2}]", element, sChildName, typeof( T ).ToString() );
    
    			T childElement = null;
    
    			//
    			// Spin through immediate children of the starting element.
    			//
    			var nChildCount = VisualTreeHelper.GetChildrenCount( element );
    			for ( int i = 0; i < nChildCount; i++ )
    			{
    				// Get next child element.
    				FrameworkElement child = VisualTreeHelper.GetChild( element, i ) as FrameworkElement;
    				Debug.WriteLine( "Found child [{0}]", child );
    
    				// Do we have a child?
    				if ( child == null )
    					continue;
    
    				// Is child of desired type and name?
    				if ( child is T && child.Name.Equals( sChildName ) )
    				{
    					// Bingo! We found a match.
    					childElement = (T) child;
    					Debug.WriteLine( "Found matching element [{0}]", childElement );
    					break;
    				} // if
    
    				// Recurse and search through this child's descendants.
    				childElement = FindElementByName<T>( child, sChildName );
    
    				// Did we find a matching child?
    				if ( childElement != null )
    					break;
    			} // for
    
    			Debug.WriteLine( "[FindElementByName] <== childElement [{0}]", childElement );
    			return childElement;
    		}

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2014年6月26日 11:16
    版主