[Ironpython-users] How to ref XAML object from IPY for WPF app?

David Bagby dave at CalypsoVentures.com
Fri Apr 27 19:28:20 CEST 2012


Hi,
I'm a bit new to both IronPython and WPF - so of course I find myself 
trying to figure out how to make them work together...
(I don't know where best to ask this question as I don't know if this is 
really a IPY or a WPF or combination question - so if there is a better 
group to consult, please tell me a good place to go ask.)

FYI, Software context:
     VS 2010
     IPY 2.7.2.1
     PTVS 1.1.1

For a learning test case, I had VS create the XAML for a window with one 
button and one label. The simple goal is to click the button and have 
the value in the label increment.

That was easy from a VB WPF app; but I can't get the same simple action 
from a IPY WPF app.

Here is the IPY XAML:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="IPY-WPF-2"Height="300"Width="300">
<Grid>
<ButtonContent="Button"Height="57"HorizontalAlignment="Left"Margin="48,26,0,0"VerticalAlignment="Top"Width="116"Click="Button_Click"DataContext="{Binding}"Name="Button1" 
/>
<LabelContent="Label"Height="38"HorizontalAlignment="Left"Margin="62,147,0,0"VerticalAlignment="Top"Width="101"Name="label1"DataContext="{BindingElementName=Label1,Path=Content.Length}" 
/>
</Grid>
</Window>

Both the IPY and VB WPF app Xaml files were created by the VS form 
editor -- so I'm guessing that the XAML is probably not the issue (but 
it's just a guess).

Here is the IPY code:

importwpf
fromSystem.Windows import Application, Window
fromSystem.Windows.Controls import Button, Label

classMyWindow(Window):
def __init__(self):
wpf.LoadComponent(self, 'IPY_WPF_2.xaml')
#label1.Content = 1

def Button_Click(self, sender, e):
#label1.Content += 1
pass

if__name__ == '__main__':
Application().Run(MyWindow())


Any attempt I make to execute either of the commented out lines that 
reference label1 cause a fault.

What is the proper way to reference a XAML object from IPY code in a WPF 
app?

Obviously, I'm having trouble referencing a control defined in XAML from 
the IPY code.
I suspect I'm missing something that should be obvious to me but isn't, 
and I'd appreciate a pointer as to what I need to do/learn.

FYI -- here the equiv VB WFP app that worked first time:
Here is the VB XAML:

<Windowx:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"Height="350"Width="525">
<Grid>
<ButtonContent="Button"Height="74"HorizontalAlignment="Left"Margin="83,31,0,0"Name="Button1"VerticalAlignment="Top"Width="162"DataContext="{BindingElementName=Button1}" 
/>
<LabelContent="Label"Height="45"HorizontalAlignment="Left"Margin="87,161,0,0"Name="Label1"VerticalAlignment="Top"Width="152" 
/>
</Grid>
</Window>

Here is the VB code:

ClassMainWindow
Public Sub New()
' This call is required by the designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call.
Label1.Content = 1
End Sub

Private Sub Button1_Click(sender As System.Object, e As 
System.Windows.RoutedEventArgs) Handles Button1.Click
Label1.Content += 1
End Sub
EndClass

The VB WPF app works as expected.

Dave
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20120427/926f0fe3/attachment.html>


More information about the Ironpython-users mailing list