A: 

That is because "1" and "0" aren't legal values for "IsChecked". It is looking for "True" or "False". You need a Value Converter to change those 1's and 0's to their boolean counterparts.

Example:

public class IntToBoolConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        int intValue = (int)value;
        return (intValue != 0);  //returns true for any non-zero value         
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new Exception("The method or operation is not implemented.");
    }
}

Then in your binding, include the converter:

<CheckBox IsChecked="{Binding XPath=@test1, Converter={StaticResource IntToBoolConverter}}"/>
<CheckBox IsChecked="{Binding XPath=@test2, Converter={StaticResource IntToBoolConverter}}"/>
Ben Collier
It doesn't seem to work. I added this in Window:xmlns:mynamespace="clr-namespace:WPFMyApp"Then in my Grid:<Grid.Resources> <mynamespace:IntToBoolConverter x:Key="myconv" /></Grid.Resources>And my checkbox:<CheckBox Height="15" Width="14" IsChecked="{Binding XPath=@test1, Converter={StaticResource myconv}}"></CheckBox>Using the debugger, I can see that Convert never gets called
Martin
Hmm, I'm getting it to work with the same declaration style that you are using. I will keep playing with it to see if I can get it to break for me too.
Ben Collier
I see what you're saying. I was using a regular int property for quick testing, but switching to XmlElement stops this from working. I suspect it has to do with XmlElement not notifying on property change. If I check the checkbox, the XmlElement Attribute changes, but if I change the attribute, the checkbox doesn't get notified.
Ben Collier
If I could vote I would go ahead and downrate my answer. :P I can't seem to get this to work with an XmlElement either. Sorry for the wasted effort! I also tried adding "Mode=TwoWay" to the binding, and tried re-writing the converter as a StringToBool converter since I started to suspect the xml attribute would be sent as a string, but nothing changed. I will update if I find a different solution.
Ben Collier
+1  A: 

I dont know if this is related to your issue, but there is currently a bug with the standard checkbox in WPF - when if you bind to the IsChecked value, it only triggers when the checkbox is checked and not when it is unchecked regardless of two way binding etc, which can cause some unexpected results. I believe this will be fixed in .Net v4 but it cost me tons of time scratching my head.

Anyhow, I took Ben's example and just expanded on it a bit more and it seems to be working fine with XElement. see below...

I made a converter class

public class IntToBoolConverter : IValueConverter
{    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)    
    {
        int intValue = System.Convert.ToInt32(value);
        return (intValue != 0);  //returns true for any non-zero value                         
    }    

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)    
    {
        if ((bool) value == true)
            return "1";
        else
            return "0";

    }
}

Then in the XAML of the window place with the corresponsing namespace references

<Window.Resources>
    <local:IntToBoolConverter x:Key="CheckBoxConv"  />
</Window.Resources>
<Grid Name="myGrid">
    <StackPanel>
        <TextBox Text="{Binding XPath=@test1}" Height="30" Width="200"/>
        <TextBox Text="{Binding XPath=@test2}" Height="30" Width="200"/>
        <CheckBox IsChecked="{Binding XPath=@test2, Converter={StaticResource CheckBoxConv}}"/>
    </StackPanel>
</Grid>

And then to test it, place the following in the window code behind.

    public partial class Window1 : Window
{
    private XmlDocument xmlDoc;
    private XmlElement xmlElemMux;

    public Window1()
    {
        InitializeComponent();
        xmlDoc = new XmlDocument();
        xmlElemMux = xmlDoc.CreateElement("Hello");
        xmlElemMux.SetAttribute("test1", "1");
        xmlElemMux.SetAttribute("test2", "0");

        myGrid.DataContext = xmlElemMux;            
    }
}

And it seems to be binding correctly, and changing the value of xmlElemMux accordingly if I click the Checkbox or change the Textbox. The convertback portion could do with a little bit of tidying up... but this just illustrates the example.

Mark Pearl
Mark, thanks for correcting my example! It works as expected now.
Ben Collier