我遇到了数据网格问题。当我触发RowEditEnding事件时,我收到以下错误:“双向绑定需要Path或XPath。”我现在想整天解决这个问题,但找不到解决办法。欢迎任何想法。提前谢谢!
XAML
<Window.Resources>
<staticData:AlarmFilesList x:Key="AlarmFilesList"/>
</Window.Resources>
XAML
<DataGrid x:Name="alarmProfilesDataGrid" Margin="10,60,10,50" AutoGenerateColumns="False" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserResizeRows="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding AlarmTime, NotifyOnTargetUpdated=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="Alarm Time" Width="75" MinWidth="75"/>
<DataGridTemplateColumn Header="Alarm File" Width="*" MinWidth="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding AlarmFile}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{StaticResource AlarmFilesList}" SelectedItem="{Binding AlarmFileDescription}" SelectionChanged="ComboBox_SelectionChanged"/>
<!--<ComboBox x:Name="AlarmFilesListComboBox" Text="{Binding AlarmFileDescription, NotifyOnSourceUpdated=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedValue="{Binding Id, NotifyOnSourceUpdated=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="AlarmFileDescription" SelectedValuePath="Id" Loaded="AlarmFilesListComboBox_Loaded" />-->
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
这是填充ComboBox的类
class AlarmFilesList : List<string>
{
BreakBuzzerEntities breakBuzzerContext = new BreakBuzzerEntities();
public AlarmFilesList()
{
var alarmFiles = (from af in breakBuzzerContext.AlarmFiles select af).ToList();
foreach (AlarmFile currentFile in alarmFiles)
{
this.Add(currentFile.AlarmFileDescription);
}
}
}
此处结束也是相关代码
private void getSelectedAlarmProfile(AlarmProfileDefinition selectedAP)
{
Mouse.OverrideCursor = Cursors.Wait;
List<AlarmProfileData> alarmProfileData = new List<AlarmProfileData>();
var alarmProfileList = (from alarmProfile in breakBuzzerContext.AlarmProfiles
where alarmProfile.AlarmProfileDefinitionId == selectedAP.Id
select alarmProfile).ToList().OrderBy(x => x.AlarmTime);
foreach (AlarmProfile alarmProfileItem in alarmProfileList)
{
alarmProfileData.Add(new AlarmProfileData { AlarmTime = alarmProfileItem.AlarmTime, AlarmTimeId=alarmProfileItem.Id, AlarmFile=alarmProfileItem.AlarmFile.AlarmFileDescription, AlarmFileId=alarmProfileItem.AlarmFileId });
}
alarmProfilesDataGrid.ItemsSource = null;
alarmProfilesDataGrid.ItemsSource = alarmProfileData;
alarmProfilesDataGrid.DataContext = this;
Mouse.OverrideCursor = null;
}
public class AlarmProfileData
{
public TimeSpan AlarmTime { get; set; }
public int AlarmTimeId { get; set; }
public string AlarmFile { get; set; }
public int AlarmFileId { get; set; }
}
private void alarmProfilesDataGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
var apData = alarmProfilesDataGrid.SelectedItem;
if (apData != null)
{
}
}
答案 0 :(得分:0)
如果您关注MVVM,如果您遵循经典模式,则必须添加ItemsSource="{Binding alarmProfileData}"
,然后您需要在网格中添加ItemsSource="{Binding}"
作为属性。为了表明网格是动态绑定的。因此你的网格定义将是:
<DataGrid x:Name="alarmProfilesDataGrid" ItemsSource="{Binding}" Margin="10,60,10,50" AutoGenerateColumns="False" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserResizeRows="False">