Connecting Tech Pros Worldwide Help | Site Map

WPF refer to local member variable for databinding

Rick
Guest
 
Posts: n/a
#1: Oct 10 '08
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

Martin Honnen
Guest
 
Posts: n/a
#2: Oct 10 '08

re: WPF refer to local member variable for databinding


Rick wrote:
Quote:
First of all, is there a group more specific to WPF?
No, Microsoft seems to favour web forums for any new stuff.
Quote:
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/
Rick
Guest
 
Posts: n/a
#3: Oct 10 '08

re: WPF refer to local member variable for databinding



"Martin Honnen" <mahotrash@yahoo.dewrote in message
news:uo%23Tm2sKJHA.3744@TK2MSFTNGP06.phx.gbl...
Quote:
Rick wrote:
Quote:
>First of all, is there a group more specific to WPF?
>
No, Microsoft seems to favour web forums for any new stuff.
>
Quote:
>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.

Closed Thread


Similar .NET Framework bytes