我已经在这个问题上苦苦挣扎了两个星期了。我在谷歌和Stackoverflow上搜索过,我似乎无法找到正确的答案。
通常我不想提问,除非绝对必要,但我真的无法想出这个问题!
问题如下。
我有两个ViewModel的应用程序。
一个包含可以在单独页面上创建的项目列表。
当用户返回主应用程序时,我有一个DataGrid,当我向DataGrid添加一行时,一个ComboBox列在一个列中,这个ComboBox包含来自第一个ViewModel的项目。
我想要发生的是当用户从这个ComboBox中选择一个项目时,我希望ComboBox中的值能够传递到DataGrid的当前ViewModel并更改DataGrid中另一个项目的值。
我目前有以下设置,但我仍然无法让它工作。
如果可能的话,我最好用直接的XAML来做这件事。
显然,以下XAML出于此目的是错误的。
<DataGrid Style="{StaticResource dataGridStyle}" ItemsSource="{Binding CurrentParser.InputVariables}" AutoGenerateColumns="False" Margin="6,6,35,6" Name="dgInputVarDefs">
<!--
<DataGrid.Triggers>
<EventTrigger RoutedEvent="ComboBox.SelectionChanged">
</EventTrigger>
</DataGrid.Triggers>
-->
<DataGrid.Columns>
<DataGridTextColumn Header="#" Binding="{Binding Path=number}" />
<DataGridTextColumn Header="Name" Binding="{Binding Path=name}" />
<DataGridTemplateColumn x:Name="dgtcFormatter" Header="->Formatter">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox
ItemsSource="{Binding Source={StaticResource FormatterViewModel},
Path=CurrentFormatter.formats}"
x:Name="cbFormatter" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn x:Name="dgtcFormat" Header="->Format">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox x:Name="dgtcFormatTextBox" Text="{Binding Path=format.format}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Separator" Binding="{Binding Path=separator}" />
<DataGridTextColumn Header="Preview" Binding="{Binding Path=preview, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"/>
</DataGrid.Columns>
</DataGrid>
答案 0 :(得分:1)
我不确定你的视图模型是如何设置的,但我倾向于确保每个表单只有一个视图模型。
如果你的组合框使用与datagrid相同的viewmodel,那么你可以在你的组合中使用XAML中的以下内容:
<ComboBox ItemsSource="{Binding Path=DataContext.myCollection, RelativeSource={RelativeSource
Mode=FindAncestor, AncestorType={x:Type Window}}}"/>
这可让您的组合使用与数据网格不同的ItemsSource
。