|
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 ) (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
{ 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;
}
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>
|
|
|
|
|
The code still doesn't make sense to me...
All you show is templates - no actual visual elements.
Why can't you just add a Loaded event handler to the WindowsFormsHost and
set the DataGridView's DataSource in that handler?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thank you very much for advice. That works very well now.
I´m beginner in WPF so I didn´t know about that Loaded event handler in WindowsFormsHost.
But when I set DataTemplate as ContentTemplate (this.ContentTemplate = FindResource("PresentationTemplate") as DataTemplate; ), that elements in DataTemplate can be considered as visual elements or can´t?
|
|
|
|
|
Czechtim2 wrote: I didn´t know about that Loaded event handler in WindowsFormsHost.
Any class derived from FrameworkElement has the Loaded event.
FrameworkElement is derived from UIElement, so you get all the
UIElement properties and events as well
Czechtim2 wrote: when I set DataTemplate as ContentTemplate (this.ContentTemplate = FindResource("PresentationTemplate") as DataTemplate; ), that elements in DataTemplate can be considered as visual elements or can´t?
Yes. Once you've actually created instances of the elements described by the
template, well... then you have actual instances.
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
|
Hi,
i want to change the colors of a menustrip(VS2008).
i changed the backgroundcolor and the textcolor with the designer.
the background is black and the text is grey.
it looks nice but if i hover over the menu, the submenu pops up and the style looks like the standard style.
How can i change the colors of the menuitems too ?
bye jo
|
|
|
|
|
|
Whenever I try to run the WPF application I am getting the following error:
Cannot create instance of 'Window1' defined in assembly 'C1WPFGrid, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null'. Exception has been thrown by the target of an
invocation. Error in markup file 'Window1.xaml' Line 1 Position 9.
Am I leaving out any declarations ? Please advise !
Best Wishes ....... ARIJIT
|
|
|
|
|
This normally means that there's a bit of a binding problem going on here. Take a look in the Output window to see if anything's causing a problem - also, set a breakpoint on the InitializeComponent method.
|
|
|
|
|
Arijit Manna wrote: Error in markup file 'Window1.xaml' Line 1 Position 9.
I'm curious....what does line 1 of 'Window1.xaml' look like?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
The delrations in XAML are like theis :
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="CIWPFGrid" Height="346" Width="628" xmlns:c1grid="http://schemas.componentone.com/wpf/C1DataGrid" WindowState="Maximized">
Best Wishes ....... ARIJIT
|
|
|
|
|
In addition to Pete's suggestions, I would try removing elements from the
window and adding them in one at a time until it breaks. XAML makes that
an easy process.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hello,
I am trying to convert some vector based objects into a raster bitmap.
I manage to do that, but the result is blurry, meaning, edges are blurry instead of jagged.
I assume an anti-aliasing algorithm is applied, but I do not want to be applied...
here is my simplified code in which I draw a circle and make a raster out of it to an Image Control:
DrawingVisual drawingVisual = new DrawingVisual();
DrawingContext drawingContext = drawingVisual.RenderOpen();
drawingContext.DrawEllipse(Brushes.Red, new Pen(Brushes.Red, 1), new Point(50, 50), 50, 50);
drawingContext.Close();
RenderTargetBitmap bmp = new RenderTargetBitmap(180, 180, 96, 96, PixelFormats.Pbgra32);
bmp.Render(drawingVisual);
_image.Source = bmp;
Here is an image to make it clearer.
What I want is the jagged edged circle...
Sample Image[^]
Does anyone have any idea why this happens???
Thanks!
|
|
|
|
|
Hi guys.
I wanna use a WinApp control in WPF ( Like Vista Clock[^] )
But I don't know how I can do it.
Any ideas will be appreciating .
|
|
|
|
|
use
<windowsformshost>
Place that control in this place..
</windowsformshost>
try this...
modified on Thursday, December 11, 2008 6:42 AM
|
|
|
|
|
|
Hi.
How can we get position of Mouse Cursor on Desktop in WPF and C# ?
Thank you.
|
|
|
|
|
Listen to the MouseMove event.
|
|
|
|
|
You're right, but when the Mouse Cursor is away of our window , we can't get the position of it .
I've wanted that.
|
|
|
|
|
How about the Mouse class[^]?
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
http://msdn.microsoft.com/en-us/library/ms648402(VS.85).aspx
http://www.pinvoke.net/default.aspx/user32/GetCaretPos.html
Don't be overcome by evil, but overcome evil with good
|
|
|
|
|
|
Hello.
How can we change the Window Location in WPF with C# code ?
Thank you.
|
|
|
|
|
|
Mohammad Dayyan wrote: How can we change the Window Location in WPF with C# code ?
Set its Left and Top properties.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|