wpf checkbox binding inside listview

c# entity-framework-6 mvvm wpf xaml

Question

I have 2 classes generated by Entity Framework as follows. My requirement is depending on the SystemEntity_ID in the JobEntity class, I want to bind the IsChecked property of the checkbox in the listview. The problem is right now all the checkboxes are being checked...Please help..

public partial class JobEntity
{
    public int JobEntity_ID { get; set; }
    public int Job_ID { get; set; }
    public Nullable<int> SystemEntity_ID { get; set; }
    public string EntityMigrationStatus { get; set; }
}

public partial class sp_SelectEntities_Result
{
    public Nullable<int> Project_ID { get; set; }
    public Nullable<int> Site_ID { get; set; }
    public int SystemEntity_ID { get; set; }
    public string EntityDesc { get; set; }
}

Below is my Viewmodel class:

public class EntityViewModel
{
    private List<sp_SelectEntities_Result> entitylist;
    private ObservableCollection<sp_SelectEntities_Result> _ObCollection;
    private EntityDbContext db;
    List<int> Selectedvalues = new List<int>();
    public EntityViewModel()
    {
        db = new EntityDbContext();
        entitylist = db.sp_SelectEntities().ToList();
        convert();
    }

    public void convert()
    {
        _ObCollection = new ObservableCollection<sp_SelectEntities_Result>(entitylist);
    }

    public ObservableCollection<sp_SelectEntities_Result> obcollection
    {
        get { return _ObCollection; }
        set { _ObCollection = value; }
    }

}

My view i.e. MainWindow.

<ListView x:Name="lstentities" ItemsSource="{Binding obcollection}" Height="250" VerticalAlignment="Top">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Selected">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox x:Name="chk" IsChecked="{Binding SystemEntity_ID}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn DisplayMemberBinding="{Binding EntityDesc}" Header="Entity" />
        </GridView>
    </ListView.View>
</ListView>

and in the mainwindow.cs

public MainWindow()
{
    InitializeComponent();

    EntityViewModel ev = new EntityViewModel();
    this.DataContext = ev;
}
1
0
12/17/2014 2:24:28 PM

Accepted Answer

The problem is that you are trying to bind nullable int to a boolean property. If you want this any way. You need a converter or Data Trigger.

Since I don't know what value will make the checkbox checked and what value will make the checkbox unchecked, i can write any code for your.

Hope this helps.

Kind Regards

0
9/28/2014 11:07:39 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow