WPF refer to local member variable for databinding

R

Rick

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
 
M

Martin Honnen

Rick said:
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>
 
R

Rick

Martin Honnen said:
No, Microsoft seems to favour web forums for any new stuff.


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>

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top