Click here to Skip to main content
15,922,166 members
Home / Discussions / WPF
   

WPF

 
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 
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 
I´m trying to do some applicaton where in one template I would use DataGridView and in another template I wouldn´t use DataGridView. DataTemplates have another structure (different number of rows, columns in Grid...) I tried something... I set DataSource somehow, but when I change DataTemplate and then again change to the first DataTemplate with DataGridView, DataSource isn´t set Frown | :-( Smile | :) ) (sample code below)

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"
        SizeChanged="gridView_SelectionChanged">
    <Window.Resources>
        <DataTemplate x:Key="ThisTemplate">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <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>
                <Button Height="22.5" Grid.Row="2" Name="button1" Width="75" Click="button1_ClickT1">Button</Button>
            </Grid >
        </DataTemplate>
        <DataTemplate x:Key="SecondTemplate">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Label Grid.Row="0" Grid.Column="1" Name="label1" Width="120">Second template</Label>
                <Button Height="22.5" Grid.Row="1" Name="button1" Width="75" Click="button1_Click">Button</Button>
            </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)
        {
            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;
        }

        private void gridView_SelectionChanged(object sender, EventArgs e)
        {
            this.ContentTemplate = FindResource("ThisTemplate") as DataTemplate;
            ChildTemplate = FindResource("ThisTemplate") as DataTemplate;
            ParentTemplate = FindVisualChild<contentpresenter>(This);
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this.ContentTemplate = FindResource("ThisTemplate") as DataTemplate;
        }

        private void button1_ClickT1(object sender, RoutedEventArgs e)
        {
            this.ContentTemplate = FindResource("SecondTemplate") as DataTemplate;
        }
    }
} 
</contentpresenter></childitem></childitem>

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 
QuestionVector To Raster in WPF is Blurry... Pin
pym11-Dec-08 4:01
pym11-Dec-08 4:01 
QuestionCan we use a WinApp control in WPF ? Pin
Mohammad Dayyan10-Dec-08 21:17
Mohammad Dayyan10-Dec-08 21:17 
AnswerRe: Can we use a WinApp control in WPF ? [modified] Pin
anandkb11-Dec-08 0:30
anandkb11-Dec-08 0:30 
GeneralRe: Can we use a WinApp control in WPF ? Pin
Mohammad Dayyan11-Dec-08 4:44
Mohammad Dayyan11-Dec-08 4:44 
QuestionCursor Position ? Pin
Mohammad Dayyan10-Dec-08 11:11
Mohammad Dayyan10-Dec-08 11:11 
AnswerRe: Cursor Position ? Pin
Jammer11-Dec-08 3:44
Jammer11-Dec-08 3:44 

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.