0

I can't manage to make my two way binding with the CheckBox work.

The CheckBox item source is PRESTAZIONI and contains a bool field called Check_Status with default 0.

The Data Binding code (DataClassesDataContext) is generated by the LINQtoSQL drag and drop, so it should be fine. It has all the ViewModel parts of this solution WPF CheckBox TwoWay Binding not working.

I have this XAML code:

<StackPanel Grid.Column="3" Grid.Row="3">
    <ListBox Name ="listPrest" Height="91" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <CheckBox Name="chkPrest" Width="220" IsChecked="{Binding Path=Check_Status, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                    <TextBlock TextWrapping="Wrap">
                        <TextBlock.Text>
                            <MultiBinding StringFormat="{}{0} ({1})">
                                <Binding Path="Nome" />
                                <Binding Path="Code" />
                            </MultiBinding>
                        </TextBlock.Text>
                    </TextBlock>
                </CheckBox>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</StackPanel>

In the code of the view, I initialize the source of the CheckBoxes like this:

public partial class InsertServiceView : UserControl
{
    DataClassesDataContext db = new DataClassesDataContext();
    IQueryable<PRESTAZIONI> prest = null;
    public InsertServiceView()
    {
        InitializeComponent();
        UpdatePrestList();
    }

    private void UpdatePrestList()
    {
        if (prest != null)
        {
            foreach (PRESTAZIONI p in prest)
            {
                p.Check_Status = false;
            }
        } 
        else 
        {
            prest = from p in db.PRESTAZIONI select p;
        }

        listPrest.ItemsSource = prest.ToList();
    }

    private void ButtonSave_Click(object sender, RoutedEventArgs e)
    {
        if (prest.Where(p => p.Check_Status == true).Count() == 0)
        {
            MessageBox.Show("No selections");
            return;
        }
    }
}

And even if I check the CheckBoxes this last if keeps returning true.

The one way binding used to work, but I need to update the value of Check_Status to all false because I use it also in other forms and I want them to be all false when I open this one.

I've seen a lot of questions similar to mine and applied all the solutions but still it is not working. Any suggestion would be very appreciated. Sorry for the poor English.

EDIT:

The table PRESTAZIONI has a primary key. This is the code for PRESTAZIONI generated by the drag and drop:

protected virtual void SendPropertyChanged(String propertyName)
    {
        if ((this.PropertyChanged != null))
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }


protected virtual void SendPropertyChanging()
        {
            if ((this.PropertyChanging != null))
            {
                this.PropertyChanging(this, emptyChangingEventArgs);
            }
        }

public partial class PRESTAZIONI : INotifyPropertyChanging, INotifyPropertyChanged
{

    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

    private bool _Check_Status;

    partial void OnCheck_StatusChanging(bool value);
    partial void OnCheck_StatusChanged();

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Check_Status", DbType="Bit NOT NULL")]
    public bool Check_Status
    {
        get
        {
            return this._Check_Status;
        }
        set
        {
            if ((this._Check_Status != value))
            {
                this.OnCheck_StatusChanging(value);
                this.SendPropertyChanging();
                this._Check_Status = value;
                this.SendPropertyChanged("Check_Status");
                this.OnCheck_StatusChanged();
            }
        }
    }
ALU
  • 353
  • 2
  • 4
  • 18
  • What is the PRESTAZIONI class, and how is the Check_Status property defined? – Rob Goodwin Jul 02 '19 at 21:31
  • 2
    Confirm that your `PRESTAZIONI` class implements the `INotifyPropertyChanged` interface, and in `Check_Status` property setter has send a `Check_Status` property change notification. – Mr. Squirrel.Downy Jul 03 '19 at 01:29
  • Not sure that this is your problem, but take a look at this: https://stackoverflow.com/questions/1069227/how-do-i-make-my-linq-to-sql-entity-implement-inotifypropertychanged – Lupu Silviu Jul 03 '19 at 06:05

1 Answers1

-1

I'm not really familiar with IQueryable, but could it be that your ToList() call is generating new instances of PRESTAZIONI?

In your save method you are checking the original IQueryable instance. It feels like you should be storing the List instead and checking that.

GazTheDestroyer
  • 20,722
  • 9
  • 70
  • 103