Search This Blog

Showing posts with label ItemsControl. Show all posts
Showing posts with label ItemsControl. Show all posts

Monday, November 18, 2013

Filter DataGrid WPF C# Example

WPF > ControlsItemControls > DataGridFilter

Using ICollectionView to filter datagrid. 


XAML:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         Title="MainWindow" Height="447.91" Width="514.871" Loaded="Window_Loaded">

    <Grid Margin="0,0,2,81">
        <DataGrid x:Name="dg" HorizontalAlignment="Left" VerticalAlignment="Top" Height="413" Width="507"  AutoGenerateColumns="False" Margin="0,0,-2,-76">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Id}"
                                Header="Id">
                    <DataGridTextColumn.HeaderTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding Content, RelativeSource=
                                         {RelativeSource Mode=TemplatedParent}}"/>
                                <TextBox x:Name="txtId" Width="100" TextChanged="txtName_TextChanged"  />
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTextColumn.HeaderTemplate>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding Name}"
                                Header="Name">
                    <DataGridTextColumn.HeaderTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding Content, RelativeSource=
                                         {RelativeSource Mode=TemplatedParent}}"/>
                                <TextBox x:Name="txtName" Width="100" TextChanged="txtName_TextChanged"  />
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTextColumn.HeaderTemplate>
                </DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

C# Code

namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    ///
     public class Skill
    {
        public int Years { get; set; }
        public string Name { get; set; }
    }
    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Position { get; set; }
        public List<Skill> SkillList { get; set; } 
    }
    public class Persons : ObservableCollection<Person>
    {
        public Persons()
        {
            Add(new Person() { Id = 1, Name = "Person1", Position = "Manager", SkillList = new List<Skill>() });
            Add(new Person() { Id = 2, Name = "Programmer2", Position = "Programmer", SkillList = new List<Skill>() });
            Add(new Person() { Id = 3, Name = "Person3", Position = "Programmer", SkillList = new List<Skill>() });
            Add(new Person() { Id = 4, Name = "Person4", Position = "Admin", SkillList = new List<Skill>() });
            Add(new Person() { Id = 5, Name = "Person5", Position = "Tester", SkillList = new List<Skill>() });
        }
    }
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Persons pList = new Persons();
            dg.ItemsSource = pList;
        }
        private void txtName_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox t = (TextBox)sender;
            string filter = t.Text;
            ICollectionView cv = CollectionViewSource.GetDefaultView(dg.ItemsSource);
            if (filter == "")
               cv.Filter = null;
            else
            {
                cv.Filter = o =>
                {
                    Person p = o as Person;
                    if (t.Name == "txtId")
                        return (p.Id == Convert.ToInt32(filter));
                    return (p.Name.ToUpper().StartsWith(filter.ToUpper()))  ;
                };
            }
         }
     }
}

Tuesday, November 12, 2013

WPF TreeView HierarchicalDataTemplate

WPF > ControlsItemsControl > TreeView > HierarchicalDataTemplate 

WPF TreeView HierarchicalDataTemplate binding to object with multiple child collections



XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Background="#FFF3EEEE" Loaded="Window_Loaded"
     xmlns:local="clr-namespace:WpfApplication1">

    <TreeView x:Name="tv" HorizontalAlignment="Left" Height="320" VerticalAlignment="Top" Width="517" >
        <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type local:Person}" ItemsSource="{Binding Persons}">
                <TextBlock Text="{Binding Title}" />
            </HierarchicalDataTemplate>
       </TreeView.Resources>
    </TreeView>
</Window>

C# Code:
namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    ///

    public class Person
    {
            public Person()
            {
                this.Persons = new ObservableCollection<Person>();
            }

            public string Title { get; set; }
            public ObservableCollection<Person> Persons { get; set; }
    }
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Person root = new Person() { Title = "Manager" };
            Person item1 = new Person() { Title = "Team Leader1" };
            item1.Persons.Add(new Person() { Title = "Emp 1" });
            item1.Persons.Add(new Person() { Title = "Emp 2" });
            root.Persons.Add(item1);
            root.Persons.Add(new Person() { Title = "Admin" });
            tv.ItemsSource = root.Persons; 
        }
    }
}