根据组合框中的选定值将文本框值绑定到数据网格

时间:2015-07-01 09:46:14

标签: c# wpf data-binding datagrid combobox

我对WPF很新,我不知道如何解决这个问题 我有以下DataGrid:

 <DataGrid x:Name="testDataGrid" Grid.Row="1"
              CanUserAddRows="True" CanUserReorderColumns="False" CanUserResizeRows="False" CanUserSortColumns="True"
              AutoGenerateColumns="False" 
              HorizontalAlignment="Left" VerticalAlignment="Top">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding SomeString}" Header="SomeString"/>
            <DataGridTextColumn Binding="{Binding quantity, ConverterCulture='de-DE', StringFormat={}{0:N3}}" Header="Quantity">
                <DataGridTextColumn.ElementStyle>
                    <Style TargetType="{x:Type TextBlock}">
                        <Setter Property="HorizontalAlignment" Value="Right"/>
                    </Style>
                </DataGridTextColumn.ElementStyle>
            </DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding Unit}" Header="Unit"/>
            <DataGridTextColumn Binding="{Binding shortText}" Header="ShortText"/>
            <DataGridTextColumn Binding="{Binding PositionKind}" Header="PositionKind"/>
        </DataGrid.Columns>

        <DataGrid.RowDetailsTemplate>
            <DataTemplate>
                <Grid>
                    <DataGrid CanUserAddRows="True" CanUserReorderColumns="False" CanUserResizeRows="False" CanUserSortColumns="True"
                              AutoGenerateColumns="False"
                              ItemsSource="{Binding Document}"
                              ScrollViewer.CanContentScroll="True"
                              ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto"
                              MaxHeight="300" MaxWidth="{Binding ElementName=DocumentDataGrid, Path=ActualWidth}"
                              MouseDoubleClick="DataGrid_MouseDoubleClick">

                        <DataGrid.Columns>
                            <DataGridTextColumn Binding="{Binding FNr}" Header="FNr"/>
                            <DataGridTextColumn Binding="{Binding KZ}" Header="KZ"/>
                            <DataGridTextColumn Binding="{Binding Minus}" Header="Minus"/>
                            <DataGridTextColumn Binding="{Binding Factor}" Header="Factor"/>
                            <DataGridTextColumn Binding="{Binding R1}" Header="R1"/>
                            <DataGridTextColumn Binding="{Binding Value1}" Header="Value1"/>
                            <DataGridTextColumn Binding="{Binding R2}" Header="R2"/>
                            <DataGridTextColumn Binding="{Binding Value2}" Header="Value2"/>
                            <DataGridTextColumn Binding="{Binding R3}" Header="R3"/>
                            <DataGridTextColumn Binding="{Binding Value3}" Header="Value3"/>
                            <DataGridTextColumn Binding="{Binding R4}" Header="R4"/>
                            <DataGridTextColumn Binding="{Binding Value4}" Header="Value4"/>
                            <DataGridTextColumn Binding="{Binding R5}" Header="R5"/>
                            <DataGridTextColumn Binding="{Binding Value5}" Header="Value5"/>
                            <DataGridTextColumn Header="Result"/>
                        </DataGrid.Columns>
                    </DataGrid>
                </Grid>
            </DataTemplate>
        </DataGrid.RowDetailsTemplate>
    </DataGrid>

我有一个窗口,通过双击DataGrid的每一行打开,其中包含以下组合框:

 <ComboBox x:Name="ComboBoxFormulas" SelectionChanged="ComboBoxFormulas_SelectionChanged" RenderTransformOrigin="0.333,-5.696" Grid.Column="1" Grid.Row="1" Margin="0,0,-190,0">
            <ComboBoxItem x:Name="oneD">SomeItem1</ComboBoxItem>
            <ComboBoxItem x:Name="oneP">SomeItem2</ComboBoxItem> 

根据所选的ComboBoxItem,不同的textLabels和textBox会变为可见:

<TextBox x:Name="txtBox1" Grid.Column="0" Grid.Row="1"  PreviewTextInput="TextBox_PreviewTextInput_Numeric" Grid.ColumnSpan="2">
            <TextBox.Style>
                <Style TargetType="TextBox">
                    <!--by default elements for formula input are invisible-->
                    <Setter Property="Visibility" Value="Collapsed" />
                    <Style.Triggers>
                        <!--show elements only when needed-->
                        <DataTrigger Binding="{Binding ElementName=oneD, Path=IsSelected}" Value="True">
                            <Setter Property="Visibility" Value="Visible"/>

所以会发生的事情是:用户双击数据网格中的一行,然后出现一个新窗口。他可以根据所选的comboBoxItem在textBoxes中输入值,然后单击Enter,这些值必须出现在DataGrid中。请你帮助我好吗?我真的被卡住了,我甚至不知道从哪里开始。任何帮助和建议将不胜感激 edit1:我正在谈论的Window背后的代码是这样的:

public partial class FormelAssistent : Window
{
    //public ObservableCollection<LPosition> OZList { get; set; }
    //LPosition Oz = new LPosition();

    public FormelAssistent()
    {
        InitializeComponent();
        //this.DataContext = new { OZList = OZList };
        //OZs.ItemsSource = OZList;
        //this.OZList = new ObservableCollection<LPosition>();
        //this.OZList.Add(Oz);
    }


    //switch between labels according to selected formulas
    private void ComboBoxFormulas_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        ComboBox comboBox = sender as ComboBox;
        ComboBoxItem item = comboBox.SelectedItem as ComboBoxItem;
        string content = item.Content.ToString();

        Image img = new Image();
        switch (content)
        {
            case "Formel01 - Dreieck":
                img.Source = new BitmapImage(new Uri("/Im/Formel01_Dreieck.jpg", UriKind.Relative));
                break;
            case "Formel01 - Prisma":
                img.Source = new BitmapImage(new Uri("/Im/Formel01_Prisma.jpg", UriKind.Relative));
                break;
            case "Formel02 - Dreieck":
                img.Source = new BitmapImage(new Uri("/Im/Formel02_Dreieck.jpg", UriKind.Relative));
                break; 

...

它与我的问题无关。它会根据所选的comboBoxItem更改图像。 edit2:从Window文本框中读取新行的DataGrid如下所示:DataGrid

0 个答案:

没有答案