I am trying to bind checkbox as below but when I click on it it doesn't even stay checked.Below is my model that maps to the db.I would like to get selected item using the checked checkboxes so I can eventually grab the cost fields.I have attempted to follow the code example from Programming Windows Store Apps with C#. by Matt Baxter-Reynolds and Iris Classon.I have updated my code to match answer provided by @Arnaud.The checkbox doesn't stay checked but the visibility of the button changes on clicking on the radiobuttons.
Model ServiceItem
public class ServiceItem:ModelItem
{
// key field...
[AutoIncrement(), PrimaryKey(), JsonIgnore]
public int Id { get; set; }
// other fields...
[Unique, JsonProperty("id")]
public int NativeId { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
public decimal Cost
{
get
{
return GetValue<decimal>();
}
set
{
SetValue(value);
}
}
[JsonIgnore] //prefer not to have it on db has no value
public bool IsSelected
{
get
{
return GetValue<bool>();
}
set
{
SetValue(value);
}
}
public ServiceItem()
{
}
}
Below is the Viewmodel
ServicesPage ViewModel
public class ServicesPageViewModel : ViewModel, IServicesPageViewModel
{
public ObservableCollection<ServiceItem> Items { get; private set; }
public ICommand ContinueCommand { get; private set; }
public ServicesPageViewModel()
{
}
public override void Initialize(IViewModelHost host)
{
base.Initialize(host);
// setup...
this.Items = new ObservableCollection<ServiceItem>();
this.ContinueCommand = new DelegateCommand((args) => GetSelected(args as CommandExecutionContext));
}
}
Below is the xaml ServicePage.xaml
<ListView ItemsSource="{Binding Items}"
IsItemClickEnabled="true"
Margin="10,10,10,0" TabIndex="1">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="3*"></ColumnDefinition>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<CheckBox Content="{Binding Name}"
BorderBrush="{ThemeResource AppBarBackgroundThemeBrush}"
IsChecked="{Binding IsSelected, Mode=TwoWay}"></CheckBox>
<Button Content="{Binding Cost, Mode=TwoWay}"
Visibility="{Binding IsSelected, Converter={StaticResource BooleanToVisibilityConverter}}" Grid.Column="1">
Viewmodel
public abstract class ViewModel : ModelItem, IViewModel
{
// somewhere to hold the host...
protected IViewModelHost Host { get; private set; }
//other code omitted
}
Viewmodel interface Iviewmodel
public interface IViewModel : INotifyPropertyChanged
{
void Initialize(IViewModelHost host);
//other code omitted
}
ModelItem
public abstract class ModelItem : INotifyPropertyChanged
{
private Dictionary<string, object> Values { get; set; }
protected ModelItem()
{
this.Values = new Dictionary<string, object>();
}
public event PropertyChangedEventHandler PropertyChanged;
protected T GetValue<T>([CallerMemberName] string name = null)
{
if (this.Values.ContainsKey(name))
return (T)this.Values[name];
else
return default(T);
}
protected void SetValue(object value, [CallerMemberName] string name = null)
{
// set...
this.Values[name] = value;
// notify...
this.OnPropertyChanged(new PropertyChangedEventArgs(name));
}
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
this.OnPropertyChanged(new PropertyChangedEventArgs(name));
}
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, e);
}
}