我有这个构造函数:
public partial class MachinesWorkingTime : Window
{
public List<MachineWorkingTime> valuesMachine { get; set;}
public MachinesWorkingTime(List<MachineWorkingTime> values)
{
InitializeComponent();
valuesMachine = values;
}
}
MachineWorkingTime结构:
public struct MachineWorkingTime
{
public string machineName { get; set; }
public List<string> posA;
}
这是我的XAML:
<Window x:Class="WorkingTimes.MachinesWorkingTime"
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:WorkingTimes"
mc:Ignorable="d"
Title="MachinesWorkingTime" Height="178.243" Width="502.092">
<Grid Background="LightGray" VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<DataGrid ItemsSource="{Binding valuesMachine}">
<DataGrid.Columns>
<DataGridTextColumn MinWidth="50" Width="50" Binding="{Binding machineName}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
但我不知道为什么我在datagrid中看不到任何东西。 在此先感谢所有
答案 0 :(得分:1)
您需要将Window的DataContext设置为视图模型类。在您的情况下,窗口类用作视图模型。试试这个:
public MachinesWorkingTime(List<MachineWorkingTime> values)
{
valuesMachine = values;
DataContext = this; //Set viewmodel of window to this
InitializeComponent();
}
我建议从Window类中拆分视图模型。
当绑定为Midfielder4时,不要使用结构来保存数据:
public class MachineWorkingTime
{
public string machineName { get; set; }
public List<string> posA;
}