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 CheckBox
es 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 CheckBox
es 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();
}
}
}