2016-03-09 32 views
2

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

ListBox

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

编辑:

我认为我的问题是不够清晰。

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

我的视图模型:

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> 

+0

我做你想用2个Datagrids试试。在Datagrid中选择一个项目,并在辅助数据网格中显示关于该选定项目的信息。为此,我将第一个datagrid的'SelectionChanged'事件绑定到一个函数,在该函数中将ItemsSource设置为当前选定的Item。只是为了这种情况,你想在代码背后尝试 –

回答

1

ListBoxSelectedItems场还有,只要绑定了这一点。也支持多种选择和扩展选择!

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

谢谢,那回答了这个问题。但我现在意识到,我只想显示'DataGrid'中所选项目的'TItems'属性。我还有其他属性,我想在不同的控件中显示 - 我该怎么做? –

0

如何:

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

谢谢,但这是行不通的。 DataGrid不显示任何内容。也许它与ViewModel中的ObservableCollection有关? –

+0

这可能是因为你的dataGrid在一个堆栈面板内 - 作为一个测试,尝试把它放在与列表框相同的级别上。另外,你是否看到VS中的输出窗口有关于数据网格绑定错误的错误? – auburg

+0

我试图把'DataGrid'放在与'ListBox'相同的层次上,仍然不起作用。我在输出窗口中看不到任何错误。我需要在ListBox中设置SelectedItem吗? –

相关问题