我正在开发一个应用程序,其中需要在单个网格单元格中有一个ComboBox和一个文本框。
我写了这段代码:
<telerikGrid:GridViewDataColumn HeaderCellStyle="{StaticResource HeaderStyle}" Width="Auto" Header="Values Format">
<telerikGrid:GridViewColumn.CellStyle >
<Style TargetType="telerikGridView:GridViewCell">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="telerikGridView:GridViewCell">
<Border >
<StackPanel Orientation="Horizontal">
<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"/>
<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}"
/>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</telerikGrid:GridViewColumn.CellStyle>
使用上面的代码,我可以在同一个单元格中使用组合框和文本框。
现在网格中可能有多行,因此对于每一行,我将在一个单元格中拥有上述控件。
无论何时在组合框中更改值,都会在后面的代码中在文本框中设置一个同步值。
以下是代码:
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<TextBox>().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 && param != null)
{
//Bit
if (param.Format.Equals(IDA.Resources_WS.ValueFormat.Bit) && txtFormatValueResult.ValidateBitFormat())
{
param.FormatValue = txtFormatValueResult.Text;
}
//Matrice
if (param.Format.Equals(IDA.Resources_WS.ValueFormat.Mat) && 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) && formatedInt >= 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) && formatedInt >= 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(":") && int.TryParse(splittedText[0], out formatedInt[0]) && 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;
}
现在,我面临的问题是:
1)尽管为所有组合框都触发了SelectionChanged事件 我只更改一个特定的组合框。
2)组合框旁边的文本框未获得更新。
3)如果我删除代码: if(e.RemovedItems.Count!= 0){} 那么, 选中相应的文本框并显示默认值,但我无法更新该值,因为某些如何触发selectionChanged事件。
4)组合框的SelectedItem有时为null,即使是a 已选择默认值。
有人能为上述问题提供解决方案吗?
此外,如果有人想到可以实现相同功能的某种不同方法