Note that there are some explanatory texts on larger screens.

plurals
  1. POwpf DataGrid automatically edit cell with CustomControl at DataTemplate
    primarykey
    data
    text
    <p>I Have a wpf datagrid with some hughe rows and some editable cells on each row. </p> <p>I want to make the cells editable just when the user navigate to it, Instead using click or dbl click. something like Excel.</p> <p>1- Is it possible with Datagrid? how to perform it ?</p> <p>2- How can i do to let user commit cell changes using arrow keys ? </p> <p><strong>UPDATE:</strong></p> <pre><code>&lt;DataGrid x:Name="DataGridDatosPlanillla" Margin="5" Grid.Row="1" Width="Auto" Height="Auto" Grid.ColumnSpan="2" CanUserReorderColumns="False" AutoGenerateColumns="False" ItemsSource="{Binding Path=PlanillaDetalle}" SelectionChanged="DataGridDatosPlanillla_SelectionChanged" Style="{StaticResource ResourceKey=Datagridstyle}" KeyboardNavigation.AcceptsReturn="True" KeyboardNavigation.DirectionalNavigation="Contained" BeginningEdit="DataGridDatosPlanillla_BeginningEdit" CurrentCellChanged="DataGridDatosPlanillla_CurrentCellChanged" FocusableChanged="DataGridDatosPlanillla_FocusableChanged"&gt; &lt;DataGrid.Columns&gt; &lt;DataGridTextColumn IsReadOnly="True" Binding="{Binding Path=Orden}" ClipboardContentBinding="{x:Null}" Header="{x:Static resources:Labels.GENERAL_IdFila}" CanUserSort="False"/&gt; &lt;DataGridTextColumn IsReadOnly="True" Binding="{Binding Path=Merlin_Contratos.StrNombreUsuario}" ClipboardContentBinding="{x:Null}" Header="{x:Static resources:Labels.ACOPIO_NombreTercero}" CanUserSort="False"/&gt; &lt;DataGridTextColumn IsReadOnly="True" Binding="{Binding Path=Merlin_ConceptosFacturacion.StrDescripcionConcepto}" ClipboardContentBinding="{x:Null}" Header="{x:Static resources:Labels.ITEMS_Nombre}" CanUserSort="False"/&gt; &lt;DataGridTemplateColumn ClipboardContentBinding="{x:Null}" Header="{x:Static resources:Labels.ACOPIO_Recogida1}" CanUserSort="False"&gt; &lt;DataGridTemplateColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;controls:NumericTextBox DecimalPlaces="{Binding Path=Merlin_ConceptosFacturacion.IntPlazasDecimales}" Text="{Binding Path=QProductor1}" BorderThickness="0"/&gt; &lt;/DataTemplate&gt; &lt;/DataGridTemplateColumn.CellTemplate&gt; &lt;/DataGridTemplateColumn&gt; &lt;DataGridTemplateColumn ClipboardContentBinding="{x:Null}" Header="{x:Static resources:Labels.ACOPIO_CantidadDescontada}" CanUserSort="False" CanUserResize="False"&gt; &lt;DataGridTemplateColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;controls:NumericTextBox DecimalPlaces="{Binding Path=Merlin_ConceptosFacturacion.IntPlazasDecimales}" Text="{Binding Path=QDescontada}"/&gt; &lt;/DataTemplate&gt; &lt;/DataGridTemplateColumn.CellTemplate&gt; &lt;/DataGridTemplateColumn&gt; &lt;DataGridTemplateColumn IsReadOnly="True" ClipboardContentBinding="{x:Null}" Header="{x:Static resources:Labels.ACOPIO_TotalRecibido}" CanUserSort="False" CanUserResize="False"&gt; &lt;DataGridTemplateColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;controls:NumericTextBox DecimalPlaces="{Binding Path=Merlin_ConceptosFacturacion.IntPlazasDecimales}" Text="{Binding Path=QRecibida}"/&gt; &lt;/DataTemplate&gt; &lt;/DataGridTemplateColumn.CellTemplate&gt; &lt;/DataGridTemplateColumn&gt; &lt;/DataGrid.Columns&gt; &lt;/DataGrid&gt; </code></pre> <p>And this is my Custom control </p> <pre><code>using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; namespace Recursos.Controls { public class CustomFormat : IFormatProvider, ICustomFormatter { int DecimalPlaces = 0; public CustomFormat(int decimalPlaces) { DecimalPlaces = decimalPlaces; } public object GetFormat(Type FormatType) { if (FormatType == typeof(ICustomFormatter)) return this; else return null; } public string Format(string format, object arg, IFormatProvider formatProvider) { if (!this.Equals(formatProvider)) return null; if (string.IsNullOrEmpty(format)) format = "N"; string numericString = arg.ToString(); return numericString; } } public class NumericTextBox : TextBox { #region Formato private string previousText = ""; private bool ApplyingFormat = false; private CultureInfo _CI = new CultureInfo(CultureInfo.CurrentCulture.LCID,true); public CultureInfo CI { get { return _CI; } set { _CI = value; } } public static readonly DependencyProperty DecimlaPlacesProperty = DependencyProperty.Register( "DecimalPlaces", typeof(int), typeof(NumericTextBox), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.None, new PropertyChangedCallback(OnDecimalPlacesChanged) ) ); private static void OnDecimalPlacesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { } /// &lt;summary&gt; /// Numero de plazas decimales /// &lt;/summary&gt; public int DecimalPlaces { get { return (int)GetValue(DecimlaPlacesProperty); } set { SetValue(DecimlaPlacesProperty,value); _CI.NumberFormat.NumberDecimalDigits = value; } } public Decimal DecimalValue = 0; private string _DecimalSeparator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator; public string DecimalSeparator { get { return _DecimalSeparator; } set { _DecimalSeparator = value; _CI.NumberFormat.NumberDecimalSeparator = _DecimalSeparator; } } //public string DecimalSeparator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator; #endregion public NumericTextBox() { HorizontalContentAlignment = HorizontalAlignment.Right; DataObject.AddPastingHandler(this, OnPaste); } private void OnPaste(object sender, DataObjectPastingEventArgs dataObjectPastingEventArgs) { var isText = dataObjectPastingEventArgs.SourceDataObject.GetDataPresent(System.Windows.DataFormats.Text, true); if (isText) { var text = dataObjectPastingEventArgs.SourceDataObject.GetData(DataFormats.Text) as string; if (IsTextValid(text)) { return; } } dataObjectPastingEventArgs.CancelCommand(); } private bool IsTextValid(string enteredText) { // If keyboard insert key is in toggled mode, and the actual insert point is Decimalseparator, we must avoid to overwrite it if (SelectionStart == this.Text.IndexOf(DecimalSeparator) &amp; System.Windows.Input.Keyboard.GetKeyStates(System.Windows.Input.Key.Insert) == System.Windows.Input.KeyStates.Toggled) { SelectionStart += 1; } if (!enteredText.All(c =&gt; Char.IsNumber(c) || c == DecimalSeparator.ToCharArray()[0] || c == '-')) { return false; } //We only validation against unselected text since the selected text will be replaced by the entered text var unselectedText = this.Text.Remove(SelectionStart, SelectionLength); if ( enteredText == DecimalSeparator &amp;&amp; unselectedText.Contains(DecimalSeparator)) { // Before return false, must move cursor beside Decimal separator SelectionStart = this.Text.IndexOf(DecimalSeparator) + 1; return false; } if (enteredText == "-" &amp;&amp; unselectedText.Length &gt; 0) { return false; } return true; } private bool ApplyFormat(TextChangedEventArgs e) { if (!ApplyingFormat) { ApplyingFormat = true; int SelectionStartActual = SelectionStart; string FinallText = this.Text; if (!FinallText.Contains(DecimalSeparator) &amp; DecimalPlaces &gt; 0) { FinallText = String.Format("{0}{1}{2}", this.Text, DecimalSeparator, new string('0', DecimalPlaces)); } bool state = Decimal.TryParse(FinallText, NumberStyles.AllowCurrencySymbol | NumberStyles.AllowDecimalPoint | NumberStyles.AllowTrailingSign, _CI, out DecimalValue); DecimalValue = Math.Round(DecimalValue, DecimalPlaces); if (DecimalValue == 0) { FinallText = ""; } else { if (FinallText != DecimalValue.ToString(_CI)) { FinallText = DecimalValue.ToString(_CI); } } if (FinallText != this.Text) { this.Text = FinallText; SelectionStart = SelectionStartActual; } previousText = this.Text; ApplyingFormat = false; return state; } else { return true; } } protected override void OnTextChanged(TextChangedEventArgs e) { e.Handled = !ApplyFormat(e); base.OnTextChanged(e); } protected override void OnPreviewTextInput(System.Windows.Input.TextCompositionEventArgs e) { e.Handled = !IsTextValid(e.Text); base.OnPreviewTextInput(e); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload