<data:DataGrid x:Name="dg" ItemsSource="{Binding Mode=TwoWay, Path=**ViewModel**}" HorizontalAlignment="Stretch" Width="Auto"
HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"
AutoGenerateColumns="False"
PreparingCellForEdit="dg_PreparingCellForEdit"
BeginningEdit="dg_BeginningEdit">
<data:DataGrid.Columns>
<data:DataGridTemplateColumn Width="15">
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox x:Name="chkFlight" IsEnabled="{Binding Path=AllowEdit,Mode=TwoWay}" IsChecked="{Binding Path=Selected,Mode=TwoWay}" Style="{StaticResource CheckBoxStandardStyle}" VerticalAlignment="Center" HorizontalAlignment="Center" Tag="{Binding}" Click="chkFlight_Click"></CheckBox>
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
<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>
---Code Behind
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.
===========================================================================================
I fixed it by myself, using timer solved this issue.
private void UserControl_Loaded(object sender, RoutedEventArgs e) { if (DataContext != null) { CancelFlightViewModel viewModel = (CancelFlightViewModel)DataContext;
myDispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 500); // 500 Milliseconds
myDispatcherTimer.Tick += new EventHandler(Each_Tick);
myDispatcherTimer.Start();
}
}
private void Each_Tick(object o, EventArgs sender)
{
if (dg.CurrentColumn != null && dg.ItemsSource != null && this.ViewModel.FlightLegs != null && this.ViewModel.FlightLegs.Count != 0)
{
IEnumerable<FlightLeg> items = (IEnumerable<FlightLeg>)dg.ItemsSource;
FlightLeg[] legs = items.ToArray<FlightLeg>();
if (legs.Length != 0)
{
dg.CurrentColumn = dg.Columns[3];
for (int i = 0; i < legs.Length; i++)
{
if (legs[i].Selected)
{
dg.SelectedIndex = i;
dg.SelectedItem = legs[i];
FrameworkElement obj = (FrameworkElement)dg.Columns[3].GetCellContent(dg.SelectedItem);
System.Windows.Browser.HtmlPage.Plugin.Focus();
DataGridCell cellEdit = (DataGridCell)obj.Parent;
cellEdit.Focus();
dg.BeginEdit();
break;
}
}
myDispatcherTimer.Stop();
}
}
}
anyway, it is DataGrid, DataGrid does not support creating row, if it is other controls, you can use this way: http://blogger.forgottenskies.com/?p=329 http://consultingblogs.emc.com/davidwynne/archive/2008/12/15/silverlight-and-the-view-viewmodel-pattern.aspx