ComboBox 绑定到 DataGrid WPF 的值
我目前有一个组合框,其中包含从数据库绑定的数据:
<ComboBox x:Name="bookingComboBox"
ItemsSource="{Binding ElementName=bookingDomainDataSource, Path=Data}"
Height="20"
VerticalAlignment="Top"
Margin="387,79,114,0">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Width="150"
Text="{Binding Path=bookingName}" />
<TextBlock Text="{Binding Path=bookingDate, StringFormat=d MMMM yyyy}"
Width="100" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
我还有数据网格:
<sdk:DataGrid AutoGenerateColumns="False"
Height="200"
HorizontalAlignment="Left"
ItemsSource="{Binding Data, ElementName=bookingDomainDataSource}"
Margin="147,132,0,0"
x:Name="bookingDataGrid"
RowDetailsVisibilityMode="VisibleWhenSelected"
VerticalAlignment="Top"
Width="400">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn x:Name="bookingIdColumn"
Binding="{Binding bookingId, Mode=OneWay}"
Header="Booking Id"
IsReadOnly="True"
Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="bookingNameColumn"
Binding="{Binding bookingName}"
Header="Booking Name"
Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="bookingDateColumn"
Binding="{Binding bookingDate}"
Header="Booking Date"
Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="paymentIdColumn"
Binding="{Binding paymentId}"
Header="Payment Id"
Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="showIdColumn"
Binding="{Binding showId}"
Header="Show Id"
Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="ticketIdColumn"
Binding="{Binding ticketId}"
Header="Ticket Id"
Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="ticketQuantityColumn"
Binding="{Binding ticketQuantity}"
Header="Ticket Quantity"
Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="userIdColumn"
Binding="{Binding userId}"
Header="User Id"
Width="SizeToHeader" />
</sdk:DataGrid.Columns>
</sdk:DataGrid>
我希望数据网格更改为组合框中的行集的行。
我该怎么做?
谢谢
I currently have a Combo box that has data that is bind from a database:
<ComboBox x:Name="bookingComboBox"
ItemsSource="{Binding ElementName=bookingDomainDataSource, Path=Data}"
Height="20"
VerticalAlignment="Top"
Margin="387,79,114,0">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Width="150"
Text="{Binding Path=bookingName}" />
<TextBlock Text="{Binding Path=bookingDate, StringFormat=d MMMM yyyy}"
Width="100" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
I also have the datagrid:
<sdk:DataGrid AutoGenerateColumns="False"
Height="200"
HorizontalAlignment="Left"
ItemsSource="{Binding Data, ElementName=bookingDomainDataSource}"
Margin="147,132,0,0"
x:Name="bookingDataGrid"
RowDetailsVisibilityMode="VisibleWhenSelected"
VerticalAlignment="Top"
Width="400">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn x:Name="bookingIdColumn"
Binding="{Binding bookingId, Mode=OneWay}"
Header="Booking Id"
IsReadOnly="True"
Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="bookingNameColumn"
Binding="{Binding bookingName}"
Header="Booking Name"
Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="bookingDateColumn"
Binding="{Binding bookingDate}"
Header="Booking Date"
Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="paymentIdColumn"
Binding="{Binding paymentId}"
Header="Payment Id"
Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="showIdColumn"
Binding="{Binding showId}"
Header="Show Id"
Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="ticketIdColumn"
Binding="{Binding ticketId}"
Header="Ticket Id"
Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="ticketQuantityColumn"
Binding="{Binding ticketQuantity}"
Header="Ticket Quantity"
Width="SizeToHeader" />
<sdk:DataGridTextColumn x:Name="userIdColumn"
Binding="{Binding userId}"
Header="User Id"
Width="SizeToHeader" />
</sdk:DataGrid.Columns>
</sdk:DataGrid>
I would like the data grid to change to the row of the row set from the combobox.
How can I do this?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果所有控件都绑定到同一源,您应该能够使用以下属性:
应在 ComboBox 和 DataGrid 上设置该属性。
If all controls are bound to the same source you should be able to use the following property:
Which should be set on both the ComboBox and the DataGrid.
ItemsSource 属性必须设置为集合 (IEnumerable)。将 ItemsSource 设置为非集合对象意味着什么?它不会是物品的“来源”。
完成您所要求的操作的最简单方法是将数据网格的 ItemsSource 绑定到 ComboBox 的 SelectedItem 属性。如果您希望网格显示组合框中所选项目的子数据,请尝试类似 ItemsSource="{Binding ElementName=bookingComboBox, Path=SelectedItem.CHILD_DATA_PROPERTY}" 的操作。
The ItemsSource property must be set to a collection (IEnumerable). What would it mean to set the ItemsSource to a non-collection object? It wouldn't be a "source" of items.
The easiest way to do what you're asking for would be to bind the data grid's ItemsSource to the ComboBox's SelectedItem property. If you want the grid to show child data of the selected item in the combo box, try something like ItemsSource="{Binding ElementName=bookingComboBox, Path=SelectedItem.CHILD_DATA_PROPERTY}".