1

我正在开发一个应用程序,其中需要在网格的单个单元格中有一个组合框和一个文本框。

我写了这段代码:

<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 有时为空,即使已经选择了默认值。

任何人都可以为上述问题提供解决方案吗?

此外,如果有人想出一些不同的方法来实现相同的功能

4

0 回答 0