Wednesday, March 19, 2014

<Window x:Class="WpfDGFocus.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:core="clr-namespace:System;assembly=mscorlib"
        xmlns:local="clr-namespace:WpfDGFocus"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ObjectDataProvider x:Key="RecordValues" MethodName="GetValues"
                            ObjectType="{x:Type core:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type Type="local:RecordValues"/>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </Window.Resources>
    <Grid>
        <DataGrid AutoGenerateColumns="False"  
                  HorizontalAlignment="Stretch" 
                  x:Name="CenterGrid"
                  IsSynchronizedWithCurrentItem="True"
                  CanUserAddRows="True"
                  CanUserDeleteRows="True" 
                  FontSize="13.333"
                  SelectionUnit="Cell"
                  TabIndex="8"  SelectionMode="Single" 
                  HeadersVisibility="Column" 
                  Margin="0,0,0,-1.073"  FocusManager.FocusedElement="{Binding ElementName=cmbitem}">

            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Item Name" >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding Source={StaticResource RecordValues}}"
                                        Width="200">
                                <ComboBox.Style>
                                    <Style TargetType="{x:Type ComboBox}">
                                        <Setter Property="IsEditable" Value="True" />
                                        <Setter Property="FontSize" Value="12"/>
                                        <EventSetter Event="UIElement.GotFocus" Handler="Item_GotFocus" />
                                    </Style>
                                </ComboBox.Style>
                            </ComboBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn  x:Name="txtqty" Header="Qty" Width="*" Binding="{Binding Qty, UpdateSourceTrigger=PropertyChanged}" />
                <DataGridTextColumn  x:Name="txtrate" Header="Rate" Width="*" Binding="{Binding Rate, UpdateSourceTrigger=PropertyChanged}" />
                <DataGridTextColumn  x:Name="txtamount" Header="Amount" Width="*" Binding="{Binding Amount}"   />
            </DataGrid.Columns>

        </DataGrid>
    </Grid>
</Window>

public partial class MainWindow : Window
{
        ObservableCollection<Product> pros = new ObservableCollection<Product>();
        ObservableCollection<Record> records = new ObservableCollection<Record>();

        public MainWindow()
        {
            pros.Add(new Product() { Qty = "Good", Rate = "100%", Amount = "100" });

            InitializeComponent();

            CenterGrid.ItemsSource = pros;

            records.Add(new Record());
            records.Add(new Record());
            CenterGrid.DataContext = records;
        }

        private void Item_GotFocus(object sender, RoutedEventArgs e)
        {
            ((ComboBox)sender).IsDropDownOpen = true;
                pros.Add(new Product() { });
        }
}

public enum RecordValues
{
        Apple,
        Banana,
        Coconut,
        Dambul
}

public class Record
{
        private RecordValues _value = RecordValues.Apple;
        public RecordValues value
        {
            get { return _value; }
            set { _value = value; }
        }
}

public class Product
{
        public string Qty
        {
            get;
            set;
        }

        public string Rate
        {
            get;
            set;
        }

        public string Amount
        {
            get;
            set;
        }
}

0 comments :

Post a Comment

Powered by Blogger.

Followers

About

Popular Posts