Click here to Skip to main content
16,006,535 members
Home / Discussions / WPF
   

WPF

 
QuestionHow to override animation in wpf progress bar in indeterminate state? Pin
oyvindpe15-Dec-08 21:42
oyvindpe15-Dec-08 21:42 
AnswerRe: How to override animation in wpf progress bar in indeterminate state? Pin
Charith Jayasundara16-Dec-08 2:55
Charith Jayasundara16-Dec-08 2:55 
AnswerRe: How to override animation in wpf progress bar in indeterminate state? Pin
oyvindpe16-Dec-08 3:48
oyvindpe16-Dec-08 3:48 
QuestionStroke "erasure by points" action duplication on an InkCanvas. (WPF) Pin
Amit Regmi15-Dec-08 9:59
Amit Regmi15-Dec-08 9:59 
QuestionC# WPF how to find ContextMenu Parent Control? [modified] Pin
me0814-Dec-08 21:17
me0814-Dec-08 21:17 
QuestionMulti Line in WPF C# text box on Shift+Enter only Pin
Emir Gracanin13-Dec-08 8:59
Emir Gracanin13-Dec-08 8:59 
AnswerRe: Multi Line in WPF C# text box on Shift+Enter only Pin
Insincere Dave13-Dec-08 10:53
Insincere Dave13-Dec-08 10:53 
QuestionHow to set DataSource for DataGridView in WPF [modified] Pin
Czechtim213-Dec-08 6:44
Czechtim213-Dec-08 6:44 
I need this structure (DataGridView id DataTemplate) and I have problem with DataSource. I tried to find gridview and set DataSource in code-behind, but InvalidOperationException is showing : "This operation is valid only on elements that have this template applied."
I think I have applied this template.
Could somebody please help me how to set DataSource? DataSource can be set any way.

WpfWindow.xaml
<Window 
        x:Class="WPFAppl.WpfWindow" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 	
        xmlns:local="clr-namespace:WPFAppl" 
        Title="Title"  
        Height="200"  
        Width="400"          
        WindowStartupLocation="CenterScreen"  
        Name="This"          
        Loaded="UserControl_Loaded">
    <window.resources>
        <datatemplate x:key="ThisTemplate"> 
           <grid> 
                <grid.rowdefinitions> 
                    <rowdefinition height="*" /> 
                </grid.rowdefinitions> 
                <stackpanel orientation="Horizontal">
                            Grid.Row="0"> 
                    <windowsformshost width="360">
                                      xmlns="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"> 
                        <datagridview x:name="gridView"> 
                                      AllowUserToAddRows="False" 
                                      SelectionMode="FullRowSelect" 
                                      EditMode="EditProgrammatically" 
                                      xmlns="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms">
                        </datagridview >
                    </windowsformshost > 
                </stackpanel> 
            </grid> 
        </datatemplate > 
    </window.resources > 
</window >



WpfWindow.xaml.cs
using System;    
using System.Collections.Generic;    
using System.Linq;   
using System.Text;   
using System.Windows;    
using System.Windows.Controls;   
using System.Windows.Data;   
using System.Windows.Documents;      
using System.Windows.Input;      
using System.Windows.Media;      
using System.Windows.Media.Imaging;      
using System.Windows.Navigation;     
using System.Windows.Shapes;     
using System.ComponentModel;     
using System.Collections.ObjectModel;    
using System.Xml;    
using System.Data;   
using System.Globalization; 
using System.Windows.Threading; 
using System.Threading; 
 
namespace WPFAppl 
{       /// <summary>        
    /// Interaction logic for Window1.xaml       
    /// </summary>       
    public partial class WpfWindow : Window 
    { 
        public WpfWindow()  
        {  
            InitializeComponent();  
        } 
 
        private DataTemplate ChildTemplate; 
        private ContentPresenter ParentTemplate; 
        private DataTable dt; 
 
        private void UserControl_Loaded(object sender, RoutedEventArgs e) 
        { 
            this.ContentTemplate = FindResource("ThisTemplate") as DataTemplate; 
            ChildTemplate = FindResource("ThisTemplate") as DataTemplate; 
            ParentTemplate = FindVisualChild<contentpresenter>(This); 
            dt = new DataTable(); 
            dt.Columns.Add("attr1", typeof(string)); 
            dt.Columns.Add("attr2", typeof(string)); 
            dt.Columns.Add("attr3", typeof(string)); 
            System.Windows.Forms.DataGridView gw1 = ChildTemplate.FindName("gridView", ParentTemplate) as System.Windows.Forms.DataGridView; 
            gw1.DataSource = dt; 
        } 
 
        //find element in visual tree 
        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; 
        } 
    } 
}  </childitem></childitem></contentpresenter>


modified on Saturday, December 13, 2008 5:24 PM

AnswerRe: How to set DataSource for DataGridView in WPF Pin
Mark Salsbery13-Dec-08 9:34
Mark Salsbery13-Dec-08 9:34 
GeneralRe: How to set DataSource for DataGridView in WPF Pin
Czechtim213-Dec-08 11:19
Czechtim213-Dec-08 11:19 
GeneralRe: How to set DataSource for DataGridView in WPF Pin
Mark Salsbery13-Dec-08 17:16
Mark Salsbery13-Dec-08 17:16 
GeneralRe: How to set DataSource for DataGridView in WPF Pin
Czechtim213-Dec-08 22:07
Czechtim213-Dec-08 22:07 
GeneralRe: How to set DataSource for DataGridView in WPF Pin
Mark Salsbery14-Dec-08 9:38
Mark Salsbery14-Dec-08 9:38 
GeneralRe: How to set DataSource for DataGridView in WPF Pin
Czechtim214-Dec-08 11:04
Czechtim214-Dec-08 11:04 
GeneralRe: How to set DataSource for DataGridView in WPF Pin
Mark Salsbery14-Dec-08 12:06
Mark Salsbery14-Dec-08 12:06 
GeneralRe: How to set DataSource for DataGridView in WPF Pin
Czechtim215-Dec-08 2:31
Czechtim215-Dec-08 2:31 
GeneralRe: How to set DataSource for DataGridView in WPF Pin
Mark Salsbery15-Dec-08 10:28
Mark Salsbery15-Dec-08 10:28 
GeneralRe: How to set DataSource for DataGridView in WPF Pin
Czechtim215-Dec-08 10:33
Czechtim215-Dec-08 10:33 
Questionchange menustrip Pin
jogisarge12-Dec-08 5:17
jogisarge12-Dec-08 5:17 
AnswerRe: change menustrip Pin
Mark Salsbery12-Dec-08 7:05
Mark Salsbery12-Dec-08 7:05 
QuestionInstance Error in xaml Pin
Arijit Manna11-Dec-08 23:15
Arijit Manna11-Dec-08 23:15 
AnswerRe: Instance Error in xaml Pin
Pete O'Hanlon12-Dec-08 1:26
mvePete O'Hanlon12-Dec-08 1:26 
AnswerRe: Instance Error in xaml Pin
Mark Salsbery12-Dec-08 7:08
Mark Salsbery12-Dec-08 7:08 
GeneralRe: Instance Error in xaml Pin
Arijit Manna12-Dec-08 18:00
Arijit Manna12-Dec-08 18:00 
GeneralRe: Instance Error in xaml Pin
Mark Salsbery13-Dec-08 6:07
Mark Salsbery13-Dec-08 6:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.