简体   繁体   中英

Get Value of other cells DataGrid WPF

Hi I have sucessfully managed to make datagrid with checkbox but i have no idea how to get the state of checkbox it is checked or unchecked.I was recommended to use INotifyPropertyChanged. of CellEditEnding event handler.Please help me i donot know how can i implement any one of these.

what i want is that check the value of checkbox which is checked/uncheked by user click. and can get the row index as well as value in first cell of that row.

My code so far is

namespace embeddatagridcheckbox
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        List<checkedBoxIte> item = new List<checkedBoxIte>();
        public MainWindow()
        {
            InitializeComponent();
            for (int i = 0; i < 5; i++)
            {
                checkedBoxIte ite = new checkedBoxIte();
                ite.MyString = i.ToString();
                ite.MyBool = false;
                item.Add(ite);
            }
            dataGrid1.ItemsSource = item;
        }
    }

    public class checkedBoxIte
    {
        public string MyString { get; set; }
        public bool MyBool { get; set; }
    }
}

XML is

<DataGrid AutoGenerateColumns="False" Height="323" HorizontalAlignment="Left" Name="dataGrid1" VerticalAlignment="Top" Width="503" BeginningEdit="dataGrid1_BeginningEdit">
            <DataGrid.Columns>
                <DataGridTextColumn Header="MyString" Binding="{Binding MyString}" />
                <DataGridCheckBoxColumn Header="MyBool" Binding="{Binding MyBool}" />
            </DataGrid.Columns>
        </DataGrid>

Where do you want to reference the CheckBox value from? Normally I would work with the DataGrid's ItemSource object directly instead of with figuring out rows/columns, but that depends on what you are trying to do.

In your example, you would find the state of the checkboxes in your list named list in the Code Behind, since that is what your datagrid is bound to.

list[0] = first row's data

list[0].MyBool = Checkbox value of first row

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM