将DataGrid绑定到ListBox所选项

时间:2016-03-09 15:19:35

标签: c# wpf mvvm

在我的应用程序中,我有ListBox绑定到我的ViewModel中的ObservableCollection。在同一个窗口中,我有一个DataGrid。我希望DataGrid显示ListBox中当前选定的项目 - 这意味着来自ObservableCollection的一个项目。怎么做(我的DataGrid怎么写?)

我的ListBox

<ListBox x:Name="listBox" HorizontalAlignment="Left" Margin="10,10,0,36.667" Width="119" ItemsSource="{Binding ReportItems}" >

修改

我认为我的问题不够明确。

我希望DataGrid显示用户在ObservableCollection中选择的ListBox项目。

我的ViewModel:

public class MainViewModel
{
    public ObservableCollection<ReportItem> ReportItems { get; set; }
    public MainViewModel()
    {
        ReportItems = new ObservableCollection<ReportItem>();
        ReportItems.Add(Example);
    }
    public ReportItem Example = new TextReportItem() { Name = "aviran", DataFile = "aviran.txt"};
}

班级ReportItem

public class ReportItem
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string DataFile { get; set; }
}

课程TextReportItemTextParcel

 public class TextReportItem : ReportItem
{
    public ObservableCollection<TextParcel> TItems { get; set; }
}
public class TextParcel
{
    char Delimiter { get; set; }
    string LineExp { get; set; }
    string Result { get; set; }
    string IgnoreLine { get; set; }
    int DesiredResultIndexInLine { get; set; }
}

我的窗口:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ReportMaker"
    xmlns:ViewModel="clr-namespace:ReportMaker.ViewModel" x:Class="ReportMaker.MainWindow"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
    <ViewModel:MainViewModel/>
</Window.DataContext>
<Grid>
    <Button x:Name="button" Content="Create" HorizontalAlignment="Right" Margin="0,0,10,10" VerticalAlignment="Bottom" Width="75"/>
    <ComboBox x:Name="comboBox" HorizontalAlignment="Left" Margin="10,0,0,10" VerticalAlignment="Bottom" Width="120"/>
    <ListBox x:Name="listBox" HorizontalAlignment="Left" Margin="10,10,0,36.667" Width="119" ItemsSource="{Binding ReportItems}" DisplayMemberPath="Name" />
    <StackPanel HorizontalAlignment="Left" Height="274" Margin="134,10,0,0" VerticalAlignment="Top" Width="375" x:Name="selectedItemStackPannel">
        <StackPanel.Resources>
            <Style x:Key="ControlBaseStyle" TargetType="{x:Type Control}">
                <Setter Property="Margin" Value="0, 10, 0, 0" />
            </Style>
        </StackPanel.Resources>
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="Name:"/>
            <TextBox Width="150"/>
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="Data File:"/>
            <TextBox Width="150"/>
        </StackPanel>
        <DataGrid Height="190" VerticalAlignment="Bottom"  x:Name="dataGridForSelectedTextItem"/>
    </StackPanel>
    <Button x:Name="button_Copy" Content="Save" HorizontalAlignment="Right" Margin="0,0,92,10" VerticalAlignment="Bottom" Width="75"/>

</Grid>

2 个答案:

答案 0 :(得分:1)

ListBox也有SelectedItems字段,只是绑定到该字段。也支持多个和扩展选择!

<DataGrid ItemsSource="{Binding ElementName=listBox, Path=SelectedItems}" />

答案 1 :(得分:0)

怎么样:

<DataGrid ItemsSource="{Binding ElementName=listBox,Path=SelectedItem}"/DataGrid>