views:

99

answers:

0

Hi,

I'm new to WPF and have been trying to get the databinding working properly. I have simplified my control a bit for this example. With the code I'm happy with the way that the databinding is working with the textbox but i can't get a similar result from the radiobutton.

Theres two things I'm trying to get the radiobutton databinding to do:

  1. I want to bind two radio buttons {yes,no} to the bool Signed
  2. Since I'm initiating them both as not checked, I want a validation error on the buttons until I have selected either yes or no.

So heres my code so far. Any suggestions on where I'm going wrong would be greatly appreciated. So far it seems that the buttons aren't linked to the Signed property at all. I should also mention that the project is compiled in visual studio 2010 for .net 4.0

public class Claimant: IDataErrorInfo, INotifyPropertyChanged 
{
    public string SiteLayout{ get; set;}
    public bool Signed { get; set; }



    #region IDataErrorInfo Members

    public string Error
    {
        get
        {

            return null;

        }
    }


    public string this[string columnName]
    {
          get
        {

            string result = null;

            if (columnName == "SiteLayout")
            {
                if (this.SiteLayout != null)
                {
                    if (this.SiteLayout.Trim() == "")
                    {
                        result = "No Site value";
                    }
                }
            }

            if (columnName == "Signed")
            {

                if (this.Signed == null)
                {
                    result = "No value for signed";
                }

            }

            return result;

        }

    }

   public event PropertyChangedEventHandler PropertyChanged;
}

and the xaml for the textbox is:

<TextBox Grid.Column="3" Grid.ColumnSpan="2" Height="23" HorizontalAlignment="Left" Margin="58,62,0,0" Name="txtBoxSiteLayout" ToolTip="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}" VerticalAlignment="Top" Width="120">
        <Binding Path="SiteLayout" Source="{StaticResource claimant}" UpdateSourceTrigger="LostFocus">
            <Binding.ValidationRules>
                <DataErrorValidationRule />
            </Binding.ValidationRules>
        </Binding>
    </TextBox>

and for the radiobutton is:

     <RadioButton 
        Content="Yes" 
        GroupName="SignedRadio" 
        Height="16" 
        HorizontalAlignment="Left" 
        Margin="58,344,0,0" 
        Name="radioSignedYes" 
        VerticalAlignment="Top" 
        Grid.Column="3" 
        Grid.ColumnSpan="2"  
        IsChecked="{Binding Path=Signed, Mode=TwoWay,Source=claimant,  Converter={StaticResource convertRadio}, ConverterParameter=true}"

    />
    <RadioButton  
        Content="No" 
        GroupName="SignedRadio" 
        Height="16" 
        HorizontalAlignment="Left" 
        Margin="74,344,0,0" 
        Name="radioSignedNo" 
        VerticalAlignment="Top" 
        Grid.Column="4" 
        IsChecked="{Binding Path=Signed, Mode=TwoWay, Source=claimant,  Converter={StaticResource convertRadio}, ConverterParameter=false}"
        />

With the converter I'm just trying a converter I found online but its not hitting any breakpoint in the class so I'm not sure it is making any difference.

[ValueConversion(typeof(bool?), typeof(bool))]
public class SuccessConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool param = bool.Parse(parameter.ToString());
        if (value == null)
        {
            return false;
        }
        else
        {
            return !((bool)value ^ param);
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool param = bool.Parse(parameter.ToString());
        return !((bool)value ^ param);
    }
}