c# - 将 DataGrid 绑定(bind)到 ListBox 选定项

标签 c# wpf mvvm

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

我的列表框:

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

编辑:

我觉得我的问题不够清楚。

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

我的 View 模型:

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>

最佳答案

ListBox 也有一个 SelectedItems 字段,只需绑定(bind)到它即可。也支持多选和扩展选择!

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

关于c# - 将 DataGrid 绑定(bind)到 ListBox 选定项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35895590/

相关文章:

c# - 如何通过 WPF 中的 DataGrid SelectedItems Linq?

c# - 在不触发 SelectionChanged 事件的情况下设置 Combobox 的选定值

javascript - 当 ViewModel 的属性重命名时 Knockout.js 绑定(bind)被破坏

c# - 想要以与保存在 C# windowsForm 中相同的格式读取从数据库检索的文件

c# - 如何在 WPF C# 中使用组合框 AddRange

c# - 我必须在我的 WPF 数据绑定(bind)中更改什么才能让列表框显示项目?

.net - 实现自己的 MVVM 还是使用 MVVM 框架?

wpf - Microsoft 示例和 WPF 规则之间不明确

javascript - 如何将 JQuery 与 C# WebBrowser 控件一起使用

c# - 如何将相同的值从列表框插入数据库