Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple controls in GridView Cell for each row
    primarykey
    data
    text
    <p>I am working on an application, in which there is a requirement to have a ComboBox and a Text Box in a single Cell of Grid.</p> <p>I have written this piece of code of this:</p> <pre><code>&lt;telerikGrid:GridViewDataColumn HeaderCellStyle="{StaticResource HeaderStyle}" Width="Auto" Header="Values Format"&gt; &lt;telerikGrid:GridViewColumn.CellStyle &gt; &lt;Style TargetType="telerikGridView:GridViewCell"&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="telerikGridView:GridViewCell"&gt; &lt;Border &gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;telerik:RadComboBox x:Name="cbxFormat" Margin="5,1,5,1" Width="50" ItemsSource="{StaticResource FormatList}" SelectedItem="{Binding Format, Mode=TwoWay}" SelectionChanged="cbxFormat_SelectionChanged" DropDownClosed="cbxFormat_DropDownClosed" LostFocus="cbxFormat_LostFocus"/&gt; &lt;TextBox x:Name="txtFormatValue" Style="{StaticResource TextBox}" Margin="0,1,5,1" Width="50" LostFocus="txtFormatValue_LostFocus" IsEnabled="False" ToolTipService.ToolTip="Bit needs a number | Mat two numbers to set the serie boundaries" Text="{Binding FormatValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /&gt; &lt;/StackPanel&gt; &lt;/Border&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;/telerikGrid:GridViewColumn.CellStyle&gt; </code></pre> <p></p> <p>With the above code i was able to have Combo box and Text Box in the same cell.</p> <p>Now there can be multiple rows in the Grid, so for each row i will be having the above controls in a single cell.</p> <p>Whenever value is changed in the combo box, a coressponding value is set in the text box from code behind.</p> <p>Here is the code for that:</p> <pre><code>private void cbxFormat_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangedEventArgs e) { if (e.RemovedItems.Count != 0) { RadComboBox cbxFormatResult = (RadComboBox)sender; TextBox txtFormatValueResult = ((StackPanel)((RadComboBox)sender).Parent).ChildrenOfType&lt;TextBox&gt;().First(); if (cbxFormatResult.SelectedItem != null) { dgParams.BeginEdit(); switch ((IDA.Resources_WS.ValueFormat)cbxFormatResult.SelectedItem) { case IDA.Resources_WS.ValueFormat.Bit: txtFormatValueResult.IsEnabled = true; txtFormatValueResult.ValidateBitFormat(); break; case IDA.Resources_WS.ValueFormat.Mat: txtFormatValueResult.IsEnabled = true; txtFormatValueResult.ValidateMatrixFormat(); break; default: txtFormatValueResult.IsEnabled = false; Parameter param = txtFormatValueResult.DataContext as Parameter; if (param != null) txtFormatValueResult.Text = param.FormatValue = string.Empty; else txtFormatValueResult.Text = string.Empty; break; } dgParams.CommitEdit(); } } } private void txtFormatValue_LostFocus(object sender, RoutedEventArgs e) { //Caller TextBox TextBox txtFormatValueResult = (TextBox)sender; //Content Parameter param = txtFormatValueResult.DataContext as Parameter; //linked controlers StackPanel stkParent = (StackPanel)txtFormatValueResult.Parent; RadComboBox cbxFormatValueResult = (RadComboBox)stkParent.Children[0]; if (!cbxFormatValueResult.IsDropDownOpen &amp;&amp; param != null) { //Bit if (param.Format.Equals(IDA.Resources_WS.ValueFormat.Bit) &amp;&amp; txtFormatValueResult.ValidateBitFormat()) { param.FormatValue = txtFormatValueResult.Text; } //Matrice if (param.Format.Equals(IDA.Resources_WS.ValueFormat.Mat) &amp;&amp; txtFormatValueResult.ValidateMatrixFormat()) { param.FormatValue = txtFormatValueResult.Text; } } } public static bool ValidateBitFormat(this TextBox txtIn) { IDA.Resources_WS.Parameter param = txtIn.DataContext as IDA.Resources_WS.Parameter; if (param != null) { int formatedInt; if (int.TryParse(txtIn.Text, out formatedInt) &amp;&amp; formatedInt &gt;= 1) { param.FormatValue = txtIn.Text; return true; } else { param.FormatValue = FormatList.DefaultBit; txtIn.Focus(); txtIn.SelectAll(); return false; } } return false; } public static bool ValidateBitFormat(this TextBox txtIn) { IDA.Resources_WS.Parameter param = txtIn.DataContext as IDA.Resources_WS.Parameter; if (param != null) { int formatedInt; if (int.TryParse(txtIn.Text, out formatedInt) &amp;&amp; formatedInt &gt;= 1) { param.FormatValue = txtIn.Text; return true; } else { param.FormatValue = FormatList.DefaultBit; txtIn.Focus(); txtIn.SelectAll(); return false; } } return false; } public static bool ValidateMatrixFormat(this TextBox txtIn) { IDA.Resources_WS.Parameter param = txtIn.DataContext as IDA.Resources_WS.Parameter; if (param != null) { int[] formatedInt = new int[2]; var splittedText = txtIn.Text.Split(':'); //is textBox contains ':' if (txtIn.Text.Contains(":") &amp;&amp; int.TryParse(splittedText[0], out formatedInt[0]) &amp;&amp; int.TryParse(splittedText[1], out formatedInt[1])) { param.FormatValue = txtIn.Text; return true; } else { param.FormatValue = FormatList.DefaultMat; txtIn.Focus(); txtIn.SelectAll(); return false; } } return false; } </code></pre> <p>Now, the problems that i am facing are:</p> <blockquote> <p>1) SelectionChanged event is fired for all the combo boxes even though i change a specific combo box only.</p> <p>2) Text box next to combo box is not getting updated.</p> <p>3) If i remove the code : <strong>if (e.RemovedItems.Count != 0){ }</strong> then, the corresponding text box is selected and also displays the default value, but i am not able to update the value, because some how the selectionChanged event is fired.</p> <p>4) SelectedItem for the combo box is sometimes null, even though a default value is already selected.</p> </blockquote> <p>Can anyone provide a solution for the above problems?</p> <p>Also, if somebody thinks of some different approach with which the same functionality can be achieved</p>
    singulars
    1. This table or related slice is empty.
    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