ComboBox 绑定到 DataGrid WPF 的值

发布于 2024-11-02 03:14:59 字数 3262 浏览 1 评论 0原文

我目前有一个组合框,其中包含从数据库绑定的数据:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黑色毁心梦 2024-11-09 03:14:59

如果所有控件都绑定到同一源,您应该能够使用以下属性:

IsSynchronizedWithCurrentItem="True"

应在 ComboBox 和 DataGrid 上设置该属性。

If all controls are bound to the same source you should be able to use the following property:

IsSynchronizedWithCurrentItem="True"

Which should be set on both the ComboBox and the DataGrid.

感性不性感 2024-11-09 03:14:59

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}".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文