【问题标题】:WPF DataGrid DataBinding From Different Lists来自不同列表的 WPF DataGrid DataBinding
【发布时间】:2018-08-04 20:49:28
【问题描述】:

请看下面的代码。我试图在同一个datagrid 中显示两个不同的列表。在我的FormulaUploadViewModel 中,我有两个不同的列表需要在datagrid 中实现。 datagrid 中的DataGridTextColumn 将从SelectedData 列表中获取值,comboBox 将从PersonList 中获取值。我设置了DataContext="{DynamicResource FormulaUploadViewModel}。谢谢。

<UserControl x:Class="SSM.Formulas.FormulaUploadView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:local="clr-namespace:SSM.Formulas"
         xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
         mc:Ignorable="d"
         MinWidth="800"
         d:DataContext="{DynamicResource FormulaUploadViewModel}">

    <TextBox Margin="10" Grid.Row="2" Grid.Column="0" Text="{Binding PersonId, Delay=500, UpdateSourceTrigger=PropertyChanged}" MaxLength="4">

    <DataGrid x:Name="selectGrid" Grid.Row="0" Grid.RowSpan="2" Grid.Column="1" ItemsSource="{Binding SelectedData}"
              AutoGenerateColumns="false" CanUserAddRows="False" IsReadOnly="True" MinHeight="700">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Flow Value" Binding="{Binding point}" />
            <DataGridTextColumn Header="Dev-Code" Binding="{Binding code}" />
            <DataGridTemplateColumn Header="Solvent">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox  ItemsSource="{Binding PersonList}" DisplayMemberPath="PersonCode" SelectedItemBinding="{Binding PersonCode, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>       
</Grid>

public class FormulaUploadViewModel : INotifyPropertyChanged
{
    public FormulaUploadViewModel()
    {
        SelectedData = new List<Data>();
        PersonList = new List<PersonList>();
        PersonId=100;
    }
    public long PersonId { get; set; }
}

【问题讨论】:

  • 另外,我正在尝试从 SelectedData 显示组合的选定项,从 PersonList 显示 itemsource。
  • 请清楚说明您的代码似乎有什么问题!你没有得到你所期望的?

标签: c# wpf data-binding wpf-controls wpfdatagrid


【解决方案1】:

您的问题是 DataGrid 中任何内容(列、列的数据模板等)的 ItemsSource 必须与 DataGrid 本身的 ItemsSource 相关。为了更清楚起见,您的“PersonList”必须是“SelectedData”对象内的一个属性。

你不能(或者至少我尝试过但没有成功)有两个不同的 DataContexts,1 个用于 DataGrid,1 个用于其中的列。

所以,这是我的示例代码:

public partial class MainWindow : Window
{    
    public ObservableCollection<Data> DataList { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;

        ObservableCollection<Person> pList = new ObservableCollection<Person>();
        pList.Add(new Person { Name = "Ma", ID = "1" });
        pList.Add(new Person { Name = "ta", ID = "2" });
        pList.Add(new Person { Name = "ha", ID = "3" });
        pList.Add(new Person { Name = "ri", ID = "4" });

        DataList = new ObservableCollection<Data>();
        DataList.Add(new Data { Point = "point1", Code = "code1", PersonList = pList });
        DataList.Add(new Data { Point = "point2", Code = "code2", PersonList = pList });
        DataList.Add(new Data { Point = "point3", Code = "code3", PersonList = pList });
    }
}

public class Data
{
    public Data()
    {
        PersonList = new ObservableCollection<Person>();
    }
    public string Point { get; set; }
    public string Code { get; set; }
    public ObservableCollection<Person> PersonList { get; set; }
}

public class Person
{
    public string Name { get; set; }
    public string ID { get; set; }
    public override string ToString()
    {
        return Name;
    }
}

XAML:

<DataGrid x:Name="myDataGrid" ItemsSource="{Binding DataList}" Grid.RowSpan="2" Grid.ColumnSpan="2" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Flow Value" Binding="{Binding Point}" />
        <DataGridTextColumn Header="Dev-Code" Binding="{Binding Code}" />
        <DataGridTemplateColumn Header="Solvent">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox ItemsSource="{Binding PersonList}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-11
    • 2020-12-06
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 2011-06-06
    • 1970-01-01
    • 2010-09-29
    相关资源
    最近更新 更多