views:

100

answers:

1

I've seen many similar questions on how to get data binding working with a checkbox, but all of the examples I've seen are in C# and I can't seem to make the leap to convert it to IronPython. I have a checkbox defined in a window thusly:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Name="Test" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">
    <DockPanel>     
        <CheckBox Name="bindtest" IsChecked="{Binding Path=o1checked, Mode=OneWay}"></CheckBox>
        <Button Content="Toggle" Name="Toggle" Padding="5"></Button>
    </DockPanel>
</Window>

And I want its IsChecked value to automatically update when self.o1checked is toggled in the following class:

class MaikoCu64(object):
    def __init__(self):
        self.o1checked = False
        ui.win['Button']['Toggle'].Click += self.Toggle_OnClick

    def Toggle_OnClick(self, sender, event):
        self.o1checked = not self.o1checked

(That ui object is a class that has the xaml loaded into it as a dictonary of ui controls. See here)

So how do I make this happen? After hours of reading through the MSDN binding documentation (also all in C#) I've tried adding this:

import System
myBinding = System.Windows.Data.Binding("o1checked")
myBinding.Source = self
myBinding.Mode = System.Windows.Data.BindingMode.OneWay
ui.win['CheckBox']['bindtest'].SetBinding(System.Windows.Controls.CheckBox.IsCheckedProperty, myBinding)

It doesn't work, but it seems like it makes at least some sense. Am I on the right track?

+2  A: 

The property should use INotifyPropertyChanged interface. See my blog for an example how to implement it in IronPython.

Also note there is a Silverlight bug in .NET or IronPython causing an error when anything else than string should be propagated back into viewmodel.

Lukas Cenovsky
Ok, this took me all week and many attempts to digest/understand/implement/test, but I finally have something similar to this working, though I am using the `__setattr__` method you mentioned in your blog for now. Thanks for posting it! Once I iron out my bugs I'll post some of my code to show how I did it. Mainly it involves inheriting from the `INotifyPropertyChanged` interface and calling `self._OnPropertyChanged` inside `__setattr__` whenever i need to tell the gui binding to update its bound target from source.
Aphex
Well, what you do in `__setter__` is exactly what the `@notify_property` decorator does. If you post your code, I can try to fix it.
Lukas Cenovsky