简体   繁体   中英

GridView CheckBox binding

How to change the value of a text block inside a grid view in WPF based on the check box selection . Grid view in WPF is populated from a sql table which has ID and Value as columns.Value here is YES or NO.I am using linq to sql . I have a check box associated to each ID in the grid view.when a user selects some rows ,i have to save the changes back to the database.

So based on the selection i have to change the value field in the row in this fashion:

If the text in the "Value" field of the grid view is "YES" then i have to change it to "NO" If the text in the "Value" field of the grid view is "NO" then i have to change it to "YES"

I am able to populate data into the gridview ,but i am not sure whether my questions in the above scenario will fit in WPF and c#.Need some guidance.

You need to do events. Click on the control and click the lightning bold and do it in the code behind in c#. The keyword is events. OnChanged, Onclicked, onrowchange, etc. is inside that properties box for that control and you change the value in the code.

Use two way binding to transfer changes from UI into database. Bind the checkboxes column to the Value field from SQL table. You will need a convertor for binding to transform from Yes/No into bool. http://msdn.microsoft.com/en-us/magazine/cc163299.aspx#S3

http://msdn.microsoft.com/en-us/library/ms752347.aspx#data_conversion

The best way to do this is to bind both the Text block and the checkbox to the same backend field in the data model and then to use code converters.

Here is a simple example.

Say you have the following simple view model with one bool property:

class SimpleViewModel: INotifyPropertyChanged
{
    private bool _checked;
    // The property to bind to
    public bool Checked
    {
        get { return _checked; }
        set { _checked = value; OnPropertyChanged("Checked");  }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));  
    }
}

Here is also a simple page with a text block and a text field that both bind to the same backend field.

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
       <local:SimpleViewModel x:Key="simpleViewModel" />
       <local:BoolToStringConverter x:Key="boolToStringConverter" />
    </Window.Resources>
    <Grid DataContext="{StaticResource simpleViewModel}">
       <StackPanel>
          <TextBlock Text="{Binding Checked, Converter={StaticResource boolToStringConverter}}" />
          <CheckBox Content="something" IsChecked="{Binding Checked}" /> 
       </StackPanel>
    </Grid> 
</Window>

Now notice that the text block binding statement contains a converter statement. Text="{Binding Checked, Converter={StaticResource boolToStringConverter}}"

The converter here is very simple. It checks the value if it's true and returns Yes, otherwise returns NO.

public class BoolToStringConverter:IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null) return null; 

        if ((bool)value == true)
            return "YES";
        else
            return "NO";
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        // this scenario is not needed since the text block is read only
        throw new NotImplementedException();
    }
}

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