在我的应用程序中,我有一个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; }
}
类TextReportItem
,TextParcel
:
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个Datagrids试试。在Datagrid中选择一个项目,并在辅助数据网格中显示关于该选定项目的信息。为此,我将第一个datagrid的'SelectionChanged'事件绑定到一个函数,在该函数中将ItemsSource设置为当前选定的Item。只是为了这种情况,你想在代码背后尝试 –