双向绑定需要 "Path" 或 "XPath" 例外。 Windows WPF
The bidirectional binding requires "Path" or "XPath" exception. Windows WPF
在看起来像这样的 Datagrid 中出现异常:
<DataGrid x:Name="ModuleGrid" AutoGenerateColumns="False" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Module Name" Binding="{Binding Path=Name}"/>
<DataGridTextColumn Header="uName" Binding="{Binding Path=UniqueName}"/>
<DataGridTemplateColumn Header="Features">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Feature.Name}"></TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Path=Features}"
SelectedItem="{Binding Path=Feature}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}"></TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
到目前为止一切正常,几乎一切正常。但是,当我 select 在我的 ComboBox 上的 Item 上时:
我得到异常:
System.InvalidOperationException: "Die bidirektionale Bindung
erfordert "Path" oder "XPath"."
背后的 C# 代码是一个对象,它包含一个功能列表 (List<Feature>
),其中包含 Feature
s。我这样初始化它:
List<Module> test_3 = pPP_2.Components.Modules.Values.Cast<Module>().ToList();
ModuleGrid.ItemsSource = test_3;
编辑:模块 Class:
class Module
{
public string Name { get; set; }
public string UniqueName { get; set; }
public Dictionary<Property, double> Properties { get; set; }
public List<Feature> Features { get; set; }
private Dictionary<Material, double> content = new Dictionary<Material, double>();
public Dictionary<Material, double> Content
{
get { return content; }
set { content = value; }
}
public double FillingCapacity { get; set; }
public double FillingAmount
{
get
{
double ret = 0;
foreach (double a in Content.Values)
{
ret += a;
}
return ret;
}
}
}
特征Class:
class Feature
{
public string Name { get; set; }
public string FeatureType { get; set; }
public Property UsedProperty { get; set; }
public double MinValue { get; set; }
public double MaxValue { get; set; }
public double ChangePerMinutePerLiter { get; set; }
}
属性 Class:
class Property
{
public string Name { get; set; }
public double DefaultValue { get; set; }
public string Unit { get; set; }
}
对于功能组合框,您似乎正在绑定到 Modules
对象上的内部下拉值,并希望将该列表更改为一个项目...可以在何处选择该项目?这毫无意义。
要纠正问题,请执行以下步骤
- 首先,您需要一个要绑定到某处的静态(不变)功能列表。
- 从
Module
中删除 Features
并将其替换为 ID 属性,现在为避免混淆,将其命名为 FeatureName
.
- 使用
DataGridComboBoxColumn
允许根据 id 更改一个值。再说一遍,可悲的是,Feature
class上的id是Name
。为了更加现实,它应该是 Feature
上的一个 Id
数字字段,它可以以这种方式识别和关联;正如 #2 中提到的,我们现在将我们的 ID 称为 FeatureName
以避免混淆。
第 1 步 此步骤在您的 window(或页面或控件)的数据连接(虚拟机?),您将从所有功能中加载 id 值,例如这些字符串:再次 { "Heinzen", "Coolen", "Ein", "Klinen", "Yagermeisters", ... }
, String
的列表(不是 Feature
)。
然后将该列表放在某个页面资源上,例如
<Window.Resources>
<CollectionViewSource x:Key="FeatureList"
Source="{Binding FeatureList}" />
</Window.Resources>
步骤 2 从 Module
中删除 public List<Feature> Features { get; set; }
并替换为 public string FeatureName { get; set; }
第 3 步 现在,您将用 更文明 [=16] 替换组合框 DataGridTemplateColumn
=].这将允许显示 和 editing/selecting 将 choose/display 特征的值。
这样写:
<DataGridComboBoxColumn Header="Feature"
SelectedItemBinding="{Binding FeatureName, Mode=TwoWay}"
ItemsSource="{Binding Source={StaticResource FeatureList}}"
Width="100" />
这应该让您在这个问题以及如何正确使用组合框方面有一个良好的开端。
在看起来像这样的 Datagrid 中出现异常:
<DataGrid x:Name="ModuleGrid" AutoGenerateColumns="False" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Module Name" Binding="{Binding Path=Name}"/>
<DataGridTextColumn Header="uName" Binding="{Binding Path=UniqueName}"/>
<DataGridTemplateColumn Header="Features">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Feature.Name}"></TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Path=Features}"
SelectedItem="{Binding Path=Feature}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}"></TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
到目前为止一切正常,几乎一切正常。但是,当我 select 在我的 ComboBox 上的 Item 上时:
我得到异常:
System.InvalidOperationException: "Die bidirektionale Bindung erfordert "Path" oder "XPath"."
背后的 C# 代码是一个对象,它包含一个功能列表 (List<Feature>
),其中包含 Feature
s。我这样初始化它:
List<Module> test_3 = pPP_2.Components.Modules.Values.Cast<Module>().ToList();
ModuleGrid.ItemsSource = test_3;
编辑:模块 Class:
class Module
{
public string Name { get; set; }
public string UniqueName { get; set; }
public Dictionary<Property, double> Properties { get; set; }
public List<Feature> Features { get; set; }
private Dictionary<Material, double> content = new Dictionary<Material, double>();
public Dictionary<Material, double> Content
{
get { return content; }
set { content = value; }
}
public double FillingCapacity { get; set; }
public double FillingAmount
{
get
{
double ret = 0;
foreach (double a in Content.Values)
{
ret += a;
}
return ret;
}
}
}
特征Class:
class Feature
{
public string Name { get; set; }
public string FeatureType { get; set; }
public Property UsedProperty { get; set; }
public double MinValue { get; set; }
public double MaxValue { get; set; }
public double ChangePerMinutePerLiter { get; set; }
}
属性 Class:
class Property
{
public string Name { get; set; }
public double DefaultValue { get; set; }
public string Unit { get; set; }
}
对于功能组合框,您似乎正在绑定到 Modules
对象上的内部下拉值,并希望将该列表更改为一个项目...可以在何处选择该项目?这毫无意义。
要纠正问题,请执行以下步骤
- 首先,您需要一个要绑定到某处的静态(不变)功能列表。
- 从
Module
中删除Features
并将其替换为 ID 属性,现在为避免混淆,将其命名为FeatureName
. - 使用
DataGridComboBoxColumn
允许根据 id 更改一个值。再说一遍,可悲的是,Feature
class上的id是Name
。为了更加现实,它应该是Feature
上的一个Id
数字字段,它可以以这种方式识别和关联;正如 #2 中提到的,我们现在将我们的 ID 称为FeatureName
以避免混淆。
第 1 步 此步骤在您的 window(或页面或控件)的数据连接(虚拟机?),您将从所有功能中加载 id 值,例如这些字符串:再次 { "Heinzen", "Coolen", "Ein", "Klinen", "Yagermeisters", ... }
, String
的列表(不是 Feature
)。
然后将该列表放在某个页面资源上,例如
<Window.Resources>
<CollectionViewSource x:Key="FeatureList"
Source="{Binding FeatureList}" />
</Window.Resources>
步骤 2 从 Module
中删除 public List<Feature> Features { get; set; }
并替换为 public string FeatureName { get; set; }
第 3 步 现在,您将用 更文明 [=16] 替换组合框 DataGridTemplateColumn
=].这将允许显示 和 editing/selecting 将 choose/display 特征的值。
这样写:
<DataGridComboBoxColumn Header="Feature"
SelectedItemBinding="{Binding FeatureName, Mode=TwoWay}"
ItemsSource="{Binding Source={StaticResource FeatureList}}"
Width="100" />
这应该让您在这个问题以及如何正确使用组合框方面有一个良好的开端。