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

Jeff Hardy jdhardy at gmail.com
Fri Apr 27 19:36:35 CEST 2012


Hi David,
There's a working smaple at
https://github.com/jdhardy/IronPythonSamples/blob/master/PyWpfSample/PyWpfSample.py
that you can check out.

>From looking at your code, I think the issue is that you need to use
'self.label1.Content = ...' to reference the controls on the form.

- Jeff

On Fri, Apr 27, 2012 at 10:28 AM, David Bagby <dave at calypsoventures.com> wrote:
> 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>
>         <Button Content="Button" Height="57" HorizontalAlignment="Left"
> Margin="48,26,0,0" VerticalAlignment="Top" Width="116" Click="Button_Click"
> DataContext="{Binding}" Name="Button1" />
>         <Label Content="Label" Height="38" HorizontalAlignment="Left"
> Margin="62,147,0,0" VerticalAlignment="Top" Width="101" Name="label1"
> DataContext="{Binding ElementName=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:
>
> import wpf
> from System.Windows import Application, Window
> from System.Windows.Controls import Button, Label
>
> class MyWindow(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:
>
> <Window x: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>
>         <Button Content="Button" Height="74" HorizontalAlignment="Left"
> Margin="83,31,0,0" Name="Button1" VerticalAlignment="Top" Width="162"
> DataContext="{Binding ElementName=Button1}" />
>         <Label Content="Label" Height="45" HorizontalAlignment="Left"
> Margin="87,161,0,0" Name="Label1" VerticalAlignment="Top" Width="152" />
>     </Grid>
> </Window>
>
> Here is the VB code:
>
> Class MainWindow
>     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
> End Class
>
> The VB WPF app works as expected.
>
> Dave
>
> _______________________________________________
> Ironpython-users mailing list
> Ironpython-users at python.org
> http://mail.python.org/mailman/listinfo/ironpython-users
>


More information about the Ironpython-users mailing list