CheckBox in WPF ListView

http://www.codeproject.com/Questions/70654/CheckBox-in-WPF-ListView


<Window x:Class="WpfApplication5.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel Margin="8">
    <ListView
      Name="listView"
      ItemsSource="{Binding}"
      >
      <ListView.View>
        <GridView>
          <GridViewColumn Header="ID" DisplayMemberBinding="{Binding ID}" />
          <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
          <GridViewColumn Header="Balance" Width="140">
            <GridViewColumn.CellTemplate>
              <DataTemplate>
                <CheckBox Tag="{Binding}" Name="Complete" IsThreeState="False" Checked="onButtonClick" />
              </DataTemplate>
            </GridViewColumn.CellTemplate>
          </GridViewColumn>
        </GridView>
      </ListView.View>
    </ListView>
    <Button Margin="4,10">GetAllEvents</Button>
  </StackPanel>
</Window>
 
i created the data table in .cs file
So i have check box in that data table
if i select that check box i have to select the corresponding row.
  Permalink       
Posted 4-Apr-10 11:30am
madhu333 196
Edited 4-Apr-10 12:56pm
Not Active 9.4K
v3
Add a Solution

Have a Question or Comment?

2 solutions

  • Top Rated
  • Most Recent
Rate:
good  

Solution 2

Hi, 
 
I think that Martin Smith looks as though he's on the right track. 
 
To satisfy your criteria of selecting the whole row, you might want to create a Dependency Property in your "Code Behind" of type "bool" and bind the "IsChecked" value of your Checkbox and the corresponding row in your data template to that value. 
 
Unless this is due to an unknown constraint I would suggest that you don't use the Checkbox as a three-state checkbox as the third state is null and this might cause problems. I might be wrong on that front though. 
 
#region CheckRow
        /// <summary>
        /// CheckRow Dependency Property
        /// </summary>
        public static readonly DependencyProperty CheckRowProperty =
            DependencyProperty.Register("CheckRow", typeof(bool), typeof(Program),
                new FrameworkPropertyMetadata((bool)false));
        /// <summary>
        /// Gets or sets the CheckRow property. This dependency property
        /// indicates the current state of the checkbox and in turn, the state of the row.
        /// </summary>
        public bool CheckRow
        {
            get { return (bool)GetValue(CheckRowProperty); }
            set { SetValue(CheckRowProperty, value); }
        }
        #endregion
 

Two things to remember when taking this approach however.
 
  • The binding in the XAML must be "TwoWay".
  • If your code behind is naive to the class containing the DataTable, you must set the data-context such that it points to where-ever your Dependency property resides. 
  < CheckBox Margin="10,0,0,0" IsChecked="{Binding Path=CheckRow, Mode="TwoWay"}"/>
 
The row in the DataTable must also be bound to this value. 
 
I hope this helps. 
 
Larrythemule
   Permalink   
v3
Have a Question or Comment?
Rate:
good  

Solution 1

Hi There,
 
I'm not 100% sure I have grasped your problem but I  think I solved your problem in a previous project of mine.
 
In your data table (the one held in your .cs file) define a boolean property that will hold the state you wish to display in a checkbox -- I'm assuming you don't need a tri-state binding here (just true/false).
 
In your xaml create a checkbox and bind the  IsChecked property to your data table property. e.g.:
 
for code:
public bool UseLogonName
        {
            get
            {
                return m_UseLogonName;
            }
            set
            {
                m_UseLogonName = value;
            }
        }
 
For xaml:
    <CheckBox Margin="10,0,0,0" IsChecked="{Binding Path=UseLogonName}">
 
Remember, you will need to ensure your property correctly raises the PropertyChangeEvent otherwise the binding wont update correctly.
 
I hope this helps,
Martin
   Permalink   

你可能感兴趣的:(CheckBox in WPF ListView)