473,399 Members | 4,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

WPF refer to local member variable for databinding

First of all, is there a group more specific to WPF?

I have a WPF form and I want to bind to a local member variable from the
xaml.

the code behind is like this:

Partial Public Class MyClass
Public Property MyProp as system.xml.XmlDocument
get...
set...
end property

Then in the xaml I want to bind to a grid like this:

<Grid.DataContext>
<XmlDataProvider Source="{MyProp}" XPath="profiles" />
</Grid.DataContext>

but I get a Intellisence notice that "type MyProp cannot be found..."

I have also tried {x:Static MyProp} which also does not work.

How do I refer to a public property in my own class?

Rick

Oct 10 '08 #1
2 12893
Rick wrote:
First of all, is there a group more specific to WPF?
No, Microsoft seems to favour web forums for any new stuff.
I have a WPF form and I want to bind to a local member variable from the
xaml.

the code behind is like this:

Partial Public Class MyClass
Public Property MyProp as system.xml.XmlDocument
get...
set...
end property

Then in the xaml I want to bind to a grid like this:

<Grid.DataContext>
<XmlDataProvider Source="{MyProp}" XPath="profiles" />
</Grid.DataContext>

but I get a Intellisence notice that "type MyProp cannot be found..."

I have also tried {x:Static MyProp} which also does not work.

How do I refer to a public property in my own class?
It is not clear what you want to achieve. The Source property of an
XmlDataProvider takes a Uri and not an XmlDocument. If you have an
XmlDocument then you would need to set the Document property, not the
Source property. I am not sure whether you can do that in XAML, here is
how you could do that in code:

Foo.vb:
Imports System.Xml

Public Class Foo
Private _doc As XmlDocument
Public Property Doc() As XmlDocument
Get
Return _doc
End Get
Set(ByVal value As XmlDocument)
_doc = value
End Set
End Property
End Class

Window1.xaml.vb:

Imports System.Xml

Class Window1

Public Sub New()

' This call is required by the Windows Form Designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call.
Dim xdp1 As XmlDataProvider = CType(Me.Resources("xdp1"),
XmlDataProvider)

Dim foo1 As New Foo()
foo1.Doc = New XmlDocument()

foo1.Doc.LoadXml("<root><item>1</item><item>2</item><item>3</item></root>")

xdp1.Document = foo1.Doc

End Sub
End Class

Window1.xaml:

<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<XmlDataProvider x:Key="xdp1" XPath="root"/>
</Window.Resources>
<StackPanel>
<ListBox ItemsSource="{Binding Source={StaticResource xdp1},
XPath=item}"></ListBox>
</StackPanel>
</Window>
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Oct 10 '08 #2

"Martin Honnen" <ma*******@yahoo.dewrote in message
news:uo****************@TK2MSFTNGP06.phx.gbl...
Rick wrote:
>First of all, is there a group more specific to WPF?

No, Microsoft seems to favour web forums for any new stuff.
>I have a WPF form and I want to bind to a local member variable from the
xaml.

the code behind is like this:

Partial Public Class MyClass
Public Property MyProp as system.xml.XmlDocument
get...
set...
end property

Then in the xaml I want to bind to a grid like this:

<Grid.DataContext>
<XmlDataProvider Source="{MyProp}" XPath="profiles" />
</Grid.DataContext>

but I get a Intellisence notice that "type MyProp cannot be found..."

I have also tried {x:Static MyProp} which also does not work.

How do I refer to a public property in my own class?

It is not clear what you want to achieve. The Source property of an
XmlDataProvider takes a Uri and not an XmlDocument. If you have an
XmlDocument then you would need to set the Document property, not the
Source property. I am not sure whether you can do that in XAML, here is
how you could do that in code:

Foo.vb:
Imports System.Xml

Public Class Foo
Private _doc As XmlDocument
Public Property Doc() As XmlDocument
Get
Return _doc
End Get
Set(ByVal value As XmlDocument)
_doc = value
End Set
End Property
End Class

Window1.xaml.vb:

Imports System.Xml

Class Window1

Public Sub New()

' This call is required by the Windows Form Designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call.
Dim xdp1 As XmlDataProvider = CType(Me.Resources("xdp1"),
XmlDataProvider)

Dim foo1 As New Foo()
foo1.Doc = New XmlDocument()

foo1.Doc.LoadXml("<root><item>1</item><item>2</item><item>3</item></root>")

xdp1.Document = foo1.Doc

End Sub
End Class

Window1.xaml:

<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<XmlDataProvider x:Key="xdp1" XPath="root"/>
</Window.Resources>
<StackPanel>
<ListBox ItemsSource="{Binding Source={StaticResource xdp1},
XPath=item}"></ListBox>
</StackPanel>
</Window>
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Thanks Martin,

Sorry, yes I need to set the Document property not the Source. I can do
this in code already, however just as an exercise I wanted to do it in xaml
since I am just learning and want to see if it is possible. There must be
some way to refer to the code-behind in the xaml, but I can't seem to find
it yet.

I'll keep looking.

Oct 10 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Seb | last post by:
Is this efficient for a header file and a class? Does a 'static local' variable get created for each instance or include of the header file? Also, 'return _type().ToString();' does not seem to...
5
by: john | last post by:
By doing: void MyClass::MyFunction() { static int myvar; .... } We can defined a function local variable 'myvar' that keeps its value from call to call (the point is that the variable can...
1
by: Oliver Corona | last post by:
I am wondering if anyone has any insights on the performance benefit (or detriment) of declaring local variables instead of referencing members. Is allocating memory for a new variable more...
9
by: Stefan Turalski \(stic\) | last post by:
Hi, I done sth like this: for(int i=0; i<10; i++) {...} and after this local declaration of i variable I try to inicialize int i=0;
3
by: Angus | last post by:
Hello I have a member variable: std::map<int, CAgentsm_AgentsList; CAgents is just a really small class with some member variables. Just really a container for agent data. Agents log in to...
5
by: lovecreatesbea... | last post by:
Does the expression *(int *)&s1 below inside the printf() statement guarantee to refer to the first member of the structure variable s1? I've tried the code and it seems that it works that way. The...
6
by: =?Utf-8?B?bGlhbnF0bGl0?= | last post by:
lets say there are 3 int ncount variable in every method private void x_method1() { int ncount = dv.length; } private void x_method2() { int ncount = drarr.length;
2
by: Rick | last post by:
First of all, is there a group more specific to WPF? I have a WPF form and I want to bind to a local member variable from the xaml. the code behind is like this: Partial Public Class...
4
by: raylopez99 | last post by:
Why is the same variable local inside a 'foreach' loop yet 'global' in scope (or to the class) outside it? RL class MyClass { int MyMemberArray1; //member variables, arrays, that are...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.