By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,987 Members | 971 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,987 IT Pros & Developers. It's quick & easy.

WPF Advice

P: n/a
Hello cSharpies,

I'm building my 1st WPF app and I'd like to get off to a good start, so
experienced advice I'm soliciting.

The application is an explorer like tool with two (2) main sections: 1)
a TreeView on the left and 2) a work Grid on the right (see XAML example
below.) The TreeView will display a hierarchy of Groups and Items; there
may be 1 or more Groups with the same set of Items. Selecting an Item
from the TreeView will display a set of controls in the work Grid. Each
Item's work Grid controls will be different. The same Items from
different groups will have the same work Grid, with different (bound) data.

For a Group and it's Items, I'd like to be able do the layout with the
designer and then use the resulting XAML as a sort of template along
with a class - having a class method load the template at runtime.

Questions:
1. Is this possible and/or advisable? If possible, how?
2. Can the XAML template be pre-compiled in some way to speed loading?
Thanks,

Bill

Example XAML:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="370" Width="579"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<DockPanel
Height="Auto" Name="dockPanel1" Width="Auto">
<StatusBar
Height="23" Name="statusBar"
Width="Auto" VerticalAlignment="Bottom"
DockPanel.Dock="Bottom" />
<Menu Height="22" Name="menu1"
Width="Auto" VerticalAlignment="Top"
DockPanel.Dock="Top">
<MenuItem Header="File">
<MenuItem Name="MenuFileExit"
Header="Exit" Click="MenuFileExit_Click" />
</MenuItem>
</Menu>
<Grid Height="Auto" Name="grid" Width="Auto">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="3"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<TreeView
Name="treeView" Height="Auto"
Grid.Column="0" Width="Auto">
<TreeViewItem Name="tviGroup" Header="Plant" >
<TreeViewItem Name="tviGroupItem1" Header="Item2" />
<TreeViewItem Name="tviGroupItem2" Header="Item2" />
<TreeViewItem Name="tviGroupItem3" Header="Item3" />
<TreeViewItem Name="tviGroupItem4" Header="Item4" />
</TreeViewItem>
</TreeView>
<GridSplitter
Name="gridSplitter" Grid.Column="1"
HorizontalAlignment="Stretch" />
<Grid
Name="workGrid"
Height="Auto" Width="Auto" Grid.Column="2" >
</Grid>
</Grid>
</DockPanel>
</Window>
Nov 20 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hello Bill,

Welcome to Microsoft Newsgroup Support Service! My name is Marco Zhou. It's
my pleasure to work with you on this thread.

1. Is this possible and/or advisable? If possible, how?

I think the design you described above is possible and workable, but I've
no idea which specific issue you are encountering when implementing this
feature/functionality, could you please elaborate a little bit?

2. Can the XAML template be pre-compiled in some way to speed loading?

Yes, you can, you could create the XAML file in visual studio, set its
"Build Action" to "Page", then Visual Studio will invoke the msbuild to
compile the XAML into lightweight/compact BAML (Binary Application Markup
Language) form, then at runtime, you could use Application.LoadComponent()
method to deserialize the BAML into CLR object graph, passing the pack URI
as parameter.

You could read more about pack URI here:
http://msdn.microsoft.com/en-us/library/aa970069.aspx

You could read more about WPF XAML compilation process here:
http://msdn.microsoft.com/en-us/library/aa970069.aspx

If you have any further questions on this issue, free feel to ask here, we
are glad to answer them.

--------------------------------------------------
Best regards,
Macro Zhou (v-*****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 20 '08 #2

P: n/a
Marco,

My issue is what features of the language to use and how to use them.

Here's an example:

In this TreeView with no items:

<TreeView
Name="treeView" Height="Auto"
Grid.Column="0" Width="Auto">
</TreeView>

that will be part of WpfApplication1.Window1

I want to have 1 or more of:

< ???? >
<TreeViewItem Name="tviGroup" Header="Plant" >
<TreeViewItem Name="tviGroupItem1" Header="Item2" />
<TreeViewItem Name="tviGroupItem2" Header="Item2" />
<TreeViewItem Name="tviGroupItem3" Header="Item3" />
<TreeViewItem Name="tviGroupItem4" Header="Item4" />
</TreeViewItem>
</????>

which can be referenced by a class in the same way that

<Window x:Class="WpfApplication1.Window1" ... >

is referenced through

namespace WpfApplication1 {
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window {
public Window1() {
InitializeComponent();
}

...
}
}

But if I do this in the XAML:

<Window x:Class="WpfApplication1.Window1" ...... </Window>
<TreeViewItem x:Class="WpfApplication1.Group">
... items ...
</TreeViewItem>

I get an error:
Error 2 'There are multiple root elements. Line 28, position 2.' XML
is not valid.

Obviously, there can be only one root element, and for the main form
Window, it needs to be <Window>. So I need to know how to structure the
XAML so that it can be part of some class other than
WpfApplication1.Window1. Do I use some sort of template feature? Do I
locate the XAML in the Window1.xaml file, or should it be in a separate
file?
Thanks

Bill

Marco Zhou [MSFT] wrote:
Hello Bill,

Welcome to Microsoft Newsgroup Support Service! My name is Marco Zhou. It's
my pleasure to work with you on this thread.

1. Is this possible and/or advisable? If possible, how?

I think the design you described above is possible and workable, but I've
no idea which specific issue you are encountering when implementing this
feature/functionality, could you please elaborate a little bit?

2. Can the XAML template be pre-compiled in some way to speed loading?

Yes, you can, you could create the XAML file in visual studio, set its
"Build Action" to "Page", then Visual Studio will invoke the msbuild to
compile the XAML into lightweight/compact BAML (Binary Application Markup
Language) form, then at runtime, you could use Application.LoadComponent()
method to deserialize the BAML into CLR object graph, passing the pack URI
as parameter.

You could read more about pack URI here:
http://msdn.microsoft.com/en-us/library/aa970069.aspx

You could read more about WPF XAML compilation process here:
http://msdn.microsoft.com/en-us/library/aa970069.aspx

If you have any further questions on this issue, free feel to ask here, we
are glad to answer them.

--------------------------------------------------
Best regards,
Macro Zhou (v-*****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.
Nov 20 '08 #3

P: n/a
Hello Bill,

I think you might need to have a quick workthrough or tutorial on XAML to
better understand this declarative UI defintion language, as long as you
get hang of it, you will be more productive:

http://msdn.microsoft.com/en-us/library/ms752059.aspx

http://www.codeproject.com/KB/WPF/GuidedTourWPF_1.aspx

If you have any questions regarding XAML after finishing reading the
articles above, feel free to post back.

Marco
--------------------------------------------------
Best regards,
Macro Zhou (v-*****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '08 #4

P: n/a
Marco,

Awesome links! Thanks.

In the business world that I need to operate in, I am often required to
learn to fly with new technology by the seat of my pants, as it were,
while trying to implement it at the same time; not an easy thing to do!
So I already have a couple of good books on WPF & .NET 3.5 and continue
to read an develop at the same time. It's a lot to learn for a dummy
like me and hard to know what direction to go with little-to-no
experience. That's where smart experienced guys like you come in :)

So now, I do have more more specific question. But I'll leave that to
another post.
Thanks,

Bill

Marco Zhou [MSFT] wrote:
Hello Bill,

I think you might need to have a quick workthrough or tutorial on XAML to
better understand this declarative UI defintion language, as long as you
get hang of it, you will be more productive:

http://msdn.microsoft.com/en-us/library/ms752059.aspx

http://www.codeproject.com/KB/WPF/GuidedTourWPF_1.aspx

If you have any questions regarding XAML after finishing reading the
articles above, feel free to post back.

Marco
--------------------------------------------------
Best regards,
Macro Zhou (v-*****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.
Nov 21 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.