Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi all,

I have a CheckBox as a WPF Datagrid HeaderTemplate in my application. my application is basically a Email Inbox Application in that i need to handle ThreeState of CheckBox.

1. if IsChecked i need to select some items in the Datagrid (say UnReaded)
2. If UnChecked i need to select some items in the Datagrid (say Readed)
3. if ThreeState i need to select some items in the Datagrid (say Junk)

this is my Demo project code

Xaml Code

XML
<Window x:Class="WPF_TEST.MainDisplayWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WPF_TEST"
        mc:Ignorable="d" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        x:Name="WindowDisplay"
        d:DesignHeight="414" d:DesignWidth="660" >
    <Grid>
        <DataGrid AutoGenerateColumns="False" Height="250" HorizontalAlignment="Left" Margin="93,58,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="469" >
            <DataGrid.Columns>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.HeaderTemplate>
                        <DataTemplate>
                            <CheckBox Content="Type" x:Name="chkHeader" IsChecked="{Binding ElementName=WindowDisplay, Path=IsHeaderSelected, Mode=TwoWay}"  IsThreeState="True" />
                        </DataTemplate>
                    </DataGridTemplateColumn.HeaderTemplate>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox IsChecked="{Binding ElementName=WindowDisplay, Path=IsHeaderSelected, Mode=TwoWay}" Content="{Binding Name}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>


Code Behind
C#
namespace WPF_TEST
{
    /// <summary>
    /// Interaction logic for MainDisplayWindow.xaml
    /// </summary>
    public partial class MainDisplayWindow : Window
    {

        public bool? IsHeaderSelected
        {
            get { return (bool?)GetValue(IsHeaderSelectedProperty); }
            set { SetValue(IsHeaderSelectedProperty, value); }
        }
        // Using a DependencyProperty as the backing store for IsHeaderSelected.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsHeaderSelectedProperty =
            DependencyProperty.Register("IsHeaderSelected", typeof(bool?), typeof(MainDisplayWindow), new UIPropertyMetadata(null));
        
        public MainDisplayWindow()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainDisplayWindow_Loaded);
        }
        void MainDisplayWindow_Loaded(object sender, RoutedEventArgs e)
        {
            MyClassData res = new MyClassData();
            dataGrid1.ItemsSource = res;
        }
    }
    public class MyClass : List<MyClass>
    {
        public string Name { get; set; }
        public MyClass()
        {
        }
    }
    public class MyClassData : List<MyClass>
    {
        public MyClassData()
        {
            this.Add(new MyClass { Name = "Readed" });
            this.Add(new MyClass { Name = "UnReaded" });
            this.Add(new MyClass { Name = "Junk" });
        }
    }
}


any one have any Idea.
Posted

1 solution

I found this link in stackoverflow which might be useful for you.

Link
 
Share this answer
 
Comments
[no name] 6-Aug-12 10:15am    
hi thanks for reply. but i don't want to use the code behind to handle that checkbox event. is there is any way that we can handle it in the XAML itself?
Aditya Mangipudi 6-Aug-12 10:20am    
So you would like to show it on the page. Something like this http://stackoverflow.com/questions/4673570/styling-the-indeterminate-state-of-a-wpf-checkbox
[no name] 11-Aug-12 1:36am    
Ya some thinglike that but i have to try it on grid any way thanks. and sorry for the delay little busy.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900