将ListBox Selected Item数据绑定到DataGrid WPF的最佳方法

时间:2015-04-27 23:47:21

标签: wpf listbox

嗨,新的WPF程序员在这里。我似乎无法让这个工作,我希望也许你有一些想法。我想在用户定义表(ListBox)中选择一个项目时,它会将数据(ObservableCollection)填充到我的数据网格中。然后我将使用下面的“插入编辑保存”按钮进行编辑。用户定义表(ListBox)中的每个项目都有多个选项和描述。

我有一个看起来像这样的XAML:

document()

和我的模特:

<Grid HorizontalAlignment="Left" Height="505" VerticalAlignment="Top" Width="805">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="210*"/>
        <ColumnDefinition Width="233*"/>
        <ColumnDefinition Width="315*"/>
        <ColumnDefinition Width="47*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="33*"/>
        <RowDefinition Height="217*"/>
        <RowDefinition Height="86*"/>
        <RowDefinition Height="89*"/>
        <RowDefinition Height="81*"/>
    </Grid.RowDefinitions>

    <ListBox  x:Name="_listbox" HorizontalAlignment="Left" Height="103" VerticalAlignment="Top" Width="210" Margin="0,1,0,0" Grid.Row="1">
        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="IsSelected" Value="{Binding Selected}"/>
            </Style>
        </ListBox.ItemContainerStyle>

        <ListBoxItem Content="ARNotes" />
        <ListBoxItem Content="Item2"/>
        <ListBoxItem Content="Item3"/>
        <ListBoxItem Content="Item4"/>
        <ListBoxItem Content="Item5"/>
        <ListBoxItem Content="Item6"/>
    </ListBox>

    <ComboBox HorizontalAlignment="Left" Height="30" Margin="4,0,0,0" VerticalAlignment="Top" Width="257" Grid.Column="2">
        <ComboBoxItem Content="FromCompanyTable"/>
    </ComboBox>

    <DataGrid x:Name="_grid" ItemsSource="{Binding SelectedItem, ElementName=_listbox}" AutoGenerateColumns="False" Grid.ColumnSpan="2" Grid.Column="1" Margin="24,2,0,3" Grid.Row="1" >
        <DataGrid.Columns>
            <DataGridTextColumn x:Name="Choice"  Width="*" Header="Choice" Binding="{Binding choice}"/>
            <DataGridTextColumn x:Name="Descip"  Width="*" Header="Description" Binding="{Binding description}"/>
        </DataGrid.Columns>
    </DataGrid>

0 个答案:

没有答案