Hello, Pascal.
 Thank you for your help. Working. Used wpf.LoadComponent.
 Here is the complete code (case study). Can anyone come in handy.
 Still, I would like to know whether it is possible to determine the XAML
namespace environment in which IronPython loaded XAML (for example
because: xmlns: nspy = "clr-namespace: MyXAML_Py;")?

1) module proba.py:

import wpf
import clr
import clrtype
import System
from System.Windows import Application, Window, MessageBox

class IProduct(object):
        __metaclass__ = clrtype.ClrInterface

        _clrnamespace = "IronPython.Samples.ClrType"

        @property
        @clrtype.accepts()
        @clrtype.returns(str)
        def Name(self): raise RuntimeError("this should not get called")

        @property
        @clrtype.accepts()
        @clrtype.returns(float)
        def Cost(self): raise RuntimeError("this should not get called")

        @clrtype.accepts()
        @clrtype.returns(bool)
        def IsAvailable(self): raise RuntimeError("this should not get called")

class Product(IProduct):
        __metaclass__ = clrtype.ClrClass

        _clrnamespace = "IronPython.Samples.ClrType"

        _clrfields = {
                "name":str,
                "cost":float,
                "_quantity":int
        }

        CLSCompliant = clrtype.attribute(System.CLSCompliantAttribute)
        clr.AddReference("System.Xml")
        XmlRoot = clrtype.attribute(System.Xml.Serialization.XmlRootAttribute)

        _clrclassattribs = [
                # Use System.Attribute subtype directly for custom attributes 
without
arguments
                System.ObsoleteAttribute,
                # Use clrtype.attribute for custom attributes with arguments 
(either
positional, named, or both)
                CLSCompliant(False),
                XmlRoot("product", Namespace="www.contoso.com")
        ]

        def __init__(self, name, cost, quantity):
                self.name = name
                self.cost = cost
                self._quantity = quantity

        # IProduct methods
        def Name(self): return self.name
        def Cost(self): return self.cost
        def IsAvailable(self): return self.quantity != 0

        @property
        @clrtype.accepts()
        @clrtype.returns(int)
        def quantity(self): return self._quantity

        @quantity.setter
        @clrtype.accepts(int)
        @clrtype.returns()
        def quantity(self, value): self._quantity = value

        @clrtype.accepts(float)
        @clrtype.returns(float)
        def calc_total(self, discount = 0.0):
                return (self.cost - discount) * self.quantity

p = Product("SergIv", 10.0, 42)
class MyWindow(Window):
        global p
        def __init__(self):
                Cont=wpf.LoadComponent(self, 'proba.xaml')
                Cont.DataContext = p

        def button1_Click(self, sender, e):
                MessageBox.Show(p.name)



if __name__ == '__main__':
        Application().Run(MyWindow())

2) file proba.xaml:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation";
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml";
        Title="Window1" Height="300" Width="300">
    <Grid>
        <Button Name="cnop" Content="Button" Height="50"
HorizontalAlignment="Left" Margin="19,64,0,0"
VerticalAlignment="Top" Width="148" Click="button1_Click"/>
        <Label Content="{Binding Path=name}" Height="33"
HorizontalAlignment="Left" Margin="20,12,0,0"
VerticalAlignment="Top" Width="244" Name="Lab1"/>
    </Grid>
</Window>

>===================================================================
> To enable Data binding you need to set the DataContext property of the
> object loaded from xaml
>
> you could do something like this
> sr = StreamReader(path)
> userCtrl = XamlReader.Load(sr.BaseStream)
> Content = userCtrl
> userCtrl.DataContext = self
>
> -----Original Message-----
> From: ironpython-users-bounces+pascal=travobject....@python.org
> [mailto:ironpython-users-bounces+pascal=travobject....@python.org] On
> Behalf
> Of sepa...@sibmail.com
> Sent: May-17-12 11:43 AM
> To: ironpython-users@python.org
> Subject: [Ironpython-users] [Fwd: RE: How do I get access to the objects
> of
> this class which is loaded XAML]
>
> Hello,
> That is, if the load XAML, as specified below, the Label (Lab1) can be
> binding on the CLR type (p.name) is defined in proba.py (do not forget
> that
> the code is the XAML load in proba.py)?
>
> --------------------------- Original Message ---------------------------
> Тема:  RE: [Ironpython-users] How do I get access to the objects of this
> class which is loaded XAML
> От:    "Pascal Normandin" <pas...@travobject.com>
> Дата:  Чтв, Май 17, 2012 21:50
> Кому:  sepa...@sibmail.com
>        ironpython-users@python.org
> --------------------------------------------------------------------------
>
> Hello,
>
> I'm not sure about the LoadComponent method but I have used the
> XamlReader.Parse(xaml) and it works well for me.
>
> Using something like
>
> sr = StreamReader(path)
> userCtrl = XamlReader.Load(sr.BaseStream) Content = userCtrl
>
> Hope that helps;
> Pascal
>
> -----Original Message-----
> From: ironpython-users-bounces+pascal=travobject....@python.org
> [mailto:ironpython-users-bounces+pascal=travobject....@python.org] On
> Behalf
> Of sepa...@sibmail.com
> Sent: May-17-12 6:40 AM
> To: ironpython-users@python.org
> Subject: [Ironpython-users] How do I get access to the objects of this
> class
> which is loaded XAML
>
> How do I get access to the objects of this class which is loaded XAML.
> Given that the objects are presented to the CLR types?
>  What namespace should be specified?
>  The text below gives an error.
>
> 1) module proba.py:
> ....................................
> import WPF
> class MyWindow(Window):
> # p - CLR type, has a property name
>       global p
>       def __init__(self):
>               wpf.LoadComponent(self, 'proba.xaml')
>
>       def button1_Click(self, sender, e):
>               MessageBox.Show(p.name)
> ....................................
> not the entire text of the program
>
> 2) file proba.xaml:
> <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation";
>         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml";
> <!-- This part discusses the -->
>         xmlns:local="clr-namespace:proba"
> <!-- .................. -->
>         Title="Window1" Height="300" Width="300">
>     <Grid>
>         <Button Name="cnop" Click="button1_Click"/>
>         <Label Content="{Binding Source={x1:p}, Path=name}" Height="33"
> HorizontalAlignment="Left" Margin="20,12,0,0"
> VerticalAlignment="Top" Width="244" Name="Lab1"/>
>     </Grid>
> </Window>
>
> _______________________________________________
> Ironpython-users mailing list
> Ironpython-users@python.org
> http://mail.python.org/mailman/listinfo/ironpython-users
>
>
>
> _______________________________________________
> Ironpython-users mailing list
> Ironpython-users@python.org
> http://mail.python.org/mailman/listinfo/ironpython-users
>
>


_______________________________________________
Ironpython-users mailing list
Ironpython-users@python.org
http://mail.python.org/mailman/listinfo/ironpython-users

Reply via email to