How to set focus for CustCombBox in a CellEditingTemplate when entering page at the first time(MVVM

Posted by Shamin on Stack Overflow See other posts from Stack Overflow or by Shamin
Published on 2010-03-08T04:39:27Z Indexed on 2010/03/08 5:06 UTC
Read the original article Hit count: 537

Filed under:
|
|

PreparingCellForEdit="dg_PreparingCellForEdit" BeginningEdit="dg_BeginningEdit">

                    <data:DataGridTemplateColumn MinWidth="300">
                        <data:DataGridTemplateColumn.HeaderStyle>
                            <Style TargetType="primitives:DataGridColumnHeader" BasedOn="{StaticResource FOTDataGridColumnHeaderStyle}">
                                <Setter Property="ContentTemplate">
                                    <Setter.Value>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding CancelReasonText2,Source={StaticResource LabelResource}}" Style="{StaticResource TextBlockLabelStandardStyle}"/>
                                        </DataTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </data:DataGridTemplateColumn.HeaderStyle>
                        <data:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding CancelReason.CancelCodeDescription}" Style="{StaticResource TextBlockLabelStandardStyle}"/>
                            </DataTemplate>
                        </data:DataGridTemplateColumn.CellTemplate>
                        <data:DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>
                                <input:AutoCompleteBox x:Name="cBoxCancelReason" FilterMode="StartsWith" IsDropDownOpen="True" SelectedItem="{Binding CancelReason, Mode=TwoWay}" ItemsSource="{Binding CancelCodes}" ValueMemberPath="CancelCodeDescription" >
                                    <input:AutoCompleteBox.ItemTemplate>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding CancelCodeDescription}" Style="{StaticResource TextBlockLabelStandardStyle}"/>
                                        </DataTemplate>
                                    </input:AutoCompleteBox.ItemTemplate>
                                </input:AutoCompleteBox>
                            </DataTemplate>
                        </data:DataGridTemplateColumn.CellEditingTemplate>
                    </data:DataGridTemplateColumn>
                </data:DataGrid.Columns>
            </data:DataGrid>

---CodeBind

public partial class CancelFlightView : UserControl,ICancelFlightView
{
    private data.CancelCode DefaultCancelCode
    {
        get
        {
            data.CancelCode code = new data.CancelCode();
            code.CancelCd = "-1";
            code.CancelCodeDescription = "-- Select Cancel Reason --";
            return code;
        }
    }

    public CancelFlightView()
    {
        InitializeComponent();
        this.dg.LoadingRow += new EventHandler<DataGridRowEventArgs>(dg_LoadingRow);
        //this.Loaded += new RoutedEventHandler(CancelFlightView_Loaded);
    }

    void dg_LoadingRow(object sender, DataGridRowEventArgs e)
    {
        CheckBox checkBox = (CheckBox)dg.Columns[0].GetCellContent(e.Row);
        if (checkBox.IsChecked.Value)
        {
            FrameworkElement obj = (FrameworkElement)dg.Columns[1].GetCellContent(e.Row);
            System.Windows.Browser.HtmlPage.Plugin.Focus();
            DataGridCell cellEdit = (DataGridCell)obj.Parent;
            cellEdit.Focus();
            dg.BeginEdit();
        }
    }

    //private void UserControl_Loaded(object sender, RoutedEventArgs e)
    //{
    //    if (DataContext != null)
    //    {
    //        CancelFlightViewModel viewModel = (CancelFlightViewModel)DataContext;
    //        viewModel.View = this;
    //        viewModel.Grid = dg;
    //        //viewModel.InitFocus();
    //    }
    //}

    //void CancelFlightView_Loaded(object sender, RoutedEventArgs e)
    //{
    //    if (dg.SelectedItem != null)
    //    {
    //        CheckBox checkBox = (CheckBox)dg.Columns[0].GetCellContent(dg.SelectedItem);
    //        if (checkBox.IsChecked.Value)
    //        {
    //            DataGridCell cellEdit = ((DataGridCell)((System.Windows.Controls.Primitives.DataGridCellsPresenter)((DataGridCell)checkBox.Parent).Parent).Children[1]);
    //            dg.CurrentColumn = dg.Columns[1];
    //            System.Windows.Browser.HtmlPage.Plugin.Focus();
    //            cellEdit.Focus();

    //            dg.BeginEdit();
    //        }
    //    }
    //}

    public CancelFlightView(CancelFlightViewModel viewModel):this()
    {
        ViewModel = viewModel;
    }

    private void dg_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
    {              
        object obj = dg.Columns[1].GetCellContent(e.Row);
        if (obj != null && obj.GetType() == typeof(AutoCompleteBox))
        {
            AutoCompleteBox cBoxCancelReason = (AutoCompleteBox)obj;
            System.Windows.Browser.HtmlPage.Plugin.Focus();
            cBoxCancelReason.Focus();
        }
    }

    private void CustomComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

    }

    private void dg_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
    {
    }

    private void chkFlight_Click(object sender, RoutedEventArgs e)
    {
        CheckBox chkTemp = sender as CheckBox;
        if (!chkTemp.IsChecked.Value)
        {
        }
        else
        {
            DataGridCell cellEdit = ((DataGridCell)((System.Windows.Controls.Primitives.DataGridCellsPresenter)((DataGridCell)chkTemp.Parent).Parent).Children[1]);
            dg.CurrentColumn = dg.Columns[1];
            cellEdit.Focus();

            dg.BeginEdit();
        }

    }

    private void LayoutRoot_KeyUp(object sender, KeyEventArgs e)
    {
        //if (e.Key == Key.Enter)
        //{
        //}
    }


    #region ICancelFlightView Members

    public CancelFlightViewModel ViewModel
    {
        get
        {
            return DataContext as CancelFlightViewModel;
        }
        set
        {
            DataContext = value;
        }
    }

    #endregion
}

Now, when user click CheckBox, I can set focus on CustCombBox, but I can't set focus on Whose checkBox.IsChecked.Value = true when page is opened for the first time.

is it possible on MVVM pattern? Looking forward your reply, thanks very much.

© Stack Overflow or respective owner

Related posts about mvvm

Related posts about xaml