我有一个似乎没有涵盖在任何其他类似问题中的情况(大多数问题无论如何都没有答案)。
这里是:
我有以下项目可供使用:
1)一个WCF服务,它返回一个包含100多个属性的非常复杂的数据协定。我不希望创建此对象的ViewModel,因为我不想保留超过100个属性,除非绝对必要。
2)我有一个Silverlight UserControl,它将此数据协定定义为资源:
<UserControl.Resources>
<DieEstimateService:DieEstimateContract x:Key="Die" />
</UserControl.Resources>
3)我有一个数据网格,它绑定了一个名为Die.DieMetalforming的Die的集合。此集合是DieMetalformingContract对象的Array。此集合中有两列可供显示,即DiePunchFunctionDescription和DiePunchConstructionDescription属性。 DiePunchFunctionDescription具有一个名为DiePunchFunctionUID的相应键属性,同样,DiePunchConstructionDescription具有一个名为DiePunchConstructionUID的键属性。当网格处于显示模式时,它使用TextBlocks显示DiePunchFunctionDescription和DiePunchConstructionDescription字段。 DataGrid通过后面的代码绑定到Die.DieMetalforming数组:
dgDieMetalforming.ItemsSource = Die.DieMetalformings;
4)当网格处于编辑模式时,DiePunchFunction和DiePunchConstruction列都有组合框,这两个组都要绑定到Die对象的元数据集合。这些集合名为DiePunchFunctionFields和DiePunchConstructionFields。两个集合都是StandardCostFieldContract对象的数组(它们根据字段类型只有不同的数据)。 StandardCostFieldContract的显示属性为&#34; FieldDescription&#34;和#34; StandardCostFieldUID&#34;。
的关键属性5)最后,datagrid包含DieMetalformingContract的Quantity属性的第三列。在正常模式下,这是一个TextBlock,在编辑模式下,这是一个TextBox。此属性正在用于双向数据绑定。
datagrid正确绑定到数据源,正如TextBlocks的正确功能和Quantity字段的TextBox所证明的那样。但是,我不能让组合框从DiePunchFunctions或DiePunchConstructions数组加载它们的数据。我已经准备了十几篇关于这个主题的文章,并尝试了我可以找到的ItemSources绑定的每个排列,但似乎没有任何效果。这是我的数据网格定义:
<sdk:DataGrid AutoGenerateColumns="False" Height="167" HorizontalAlignment="Stretch" Margin="6,255,6,0"
Name="dgDieMetalforming" VerticalAlignment="Top" Width="Auto" Grid.ColumnSpan="4"
SelectionMode="Single" TabNavigation="Local">
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn CanUserReorder="False" CanUserResize="False"
CanUserSort="False" Header="Punch/Die Function" Width="Auto" >
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding DiePunchFunctionDescription}" Margin="4" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox Name="ddlDiePunchFunction" ItemsSource="{Binding
Path=Die.DiePunchFunctionFields, Mode=OneWay}"
SelectedItem="{Binding Path=DiePunchFunctionUID, Mode=TwoWay}"
DisplayMemberPath="FieldDescription"
SelectedValuePath="StandardCostFieldUID" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn CanUserReorder="False" CanUserResize="False"
CanUserSort="False" Header="Punch/Die Construction" Width="Auto" >
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding DiePunchConstructionDescription}" Margin="4" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox Name="ddlDiePunchFunction" ItemsSource="{Binding
Path=Die.DiePunchConstructionFields, Mode=OneWay}"
SelectedItem="{Binding Path=DiePunchConstructionUID, Mode=TwoWay}"
DisplayMemberPath="FieldDescription"
SelectedValuePath="StandardCostFieldUID" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn CanUserReorder="False" CanUserResize="False"
CanUserSort="False" Header="Quantity" Width="Auto" >
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Quantity}" Margin="4" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Name="txtQuantity" Text="{Binding Quantity, Mode=TwoWay}" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
我已经花费了太多时间在这上面,并且它已经到了我需要重新设计整个想法的地步,然后再解决此问题。如果你能推荐一个能让组合框与他们的物品资源相结合的行动方案,我会永远感激。
答案 0 :(得分:1)
我最后说“SCREW IT”来进行数据绑定,并使用代码隐藏事件来绑定组合框。新网格看起来像这样(自发布问题以来我添加了一些辅助功能)。
<sdk:DataGrid AutoGenerateColumns="False" Height="167" HorizontalAlignment="Stretch" Margin="6,255,6,0"
Name="dgDieMetalforming" VerticalAlignment="Top" Width="Auto" Grid.ColumnSpan="4"
SelectionMode="Single" TabNavigation="Local">
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn CanUserReorder="False" CanUserResize="False"
CanUserSort="False" Header="Punch/Die Function" Width="Auto" >
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding DiePunchFunctionDescription}" Margin="4" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox Name="ddlDiePunchFunction"
SelectionChanged="ddlDiePunchFunction_SelectionChanged"
Loaded="ddlDiePunchFunction_Loaded"
SelectedItem="{Binding Path=DiePunchFunctionUID, Mode=TwoWay}"
DisplayMemberPath="FieldDescription"
SelectedValuePath="StandardCostFieldUID" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn CanUserReorder="False" CanUserResize="False" CanUserSort="False"
Header="Punch/Die Construction" Width="Auto" >
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding DiePunchConstructionDescription}" Margin="4" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox Name="ddlDiePunchConstruction"
SelectionChanged="ddlDiePunchConstruction_SelectionChanged"
Loaded="ddlDiePunchFunction_Loaded"
SelectedItem="{Binding Path=DiePunchConstructionUID, Mode=TwoWay}"
DisplayMemberPath="FieldDescription"
SelectedValuePath="StandardCostFieldUID" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn CanUserReorder="False" CanUserResize="False" CanUserSort="False"
Header="Quantity" Width="Auto" >
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Quantity}" Margin="4" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Name="txtQuantity" Text="{Binding Quantity, Mode=TwoWay}" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn CanUserReorder="false" CanUserResize="False" CanUserSort="False"
Header="" Width="Auto">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Name="btnDelete" Click="btnDelete_Click">
<TextBlock>delete</TextBlock></Button>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
代码隐藏看起来像这样。
private void ddlDiePunchFunction_Loaded(object sender, RoutedEventArgs e)
{
var src = sender as ComboBox;
if (src.IsNotNull())
{
var data = src.DataContext as DieMetalformingContract;
switch (src.Name)
{
case "ddlDiePunchFunction":
src.ItemsSource = Die.DiePunchFunctionFields;
src.SelectedValue = data.DiePunchFunctionUID;
break;
case "ddlDiePunchConstruction":
src.ItemsSource = Die.DiePunchConstructionFields;
src.SelectedValue = data.DiePunchConstructionUID;
break;
}
}
}
private void ddlDiePunchFunction_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var src = sender as ComboBox;
if (src.IsNotNull())
{
var data = src.DataContext as DieMetalformingContract;
data.DiePunchFunctionUID = src.SelectedValue.ToNullableGuid() ?? Guid.Empty;
data.DiePunchFunctionDescription = (from x in Die.DiePunchFunctionFields
where x.StandardCostFieldUID == data.DiePunchFunctionUID
select x.FieldDescription).FirstOrDefault();
}
}
private void ddlDiePunchConstruction_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var src = sender as ComboBox;
if (src.IsNotNull())
{
var data = src.DataContext as DieMetalformingContract;
data.DiePunchConstructionUID = src.SelectedValue.ToNullableGuid() ?? Guid.Empty;
data.DiePunchConstructionDescription = (from x in Die.DiePunchConstructionFields
where x.StandardCostFieldUID == data.DiePunchConstructionUID
select x.FieldDescription).FirstOrDefault();
}
}
感谢所有阅读此内容的人。