简体   繁体   English

通过ComboBox绑定DataGrid(PropertyChanged,WPF,绑定)

[英]Binding DataGrid wth ComboBox (PropertyChanged, WPF, Binding)

On UserControl into xmal have ComboBox filled "Id" items: 在UserControl中,将xombo中的ComboBox填充为“ Id”项:

<ComboBox x:Name="cmbId" DisplayMemberPath="Id"/>

cs: CS:

cmbId.ItemsSource = (from q in mydata.test_views
                     select q).ToList();

I'm trying to fill data into DataGrid: 我正在尝试将数据填充到DataGrid中:

 <DataGrid x:Name="UGrid" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Auto Name" Binding="{Binding SelectedItem.AutoName, UpdateSourceTrigger=PropertyChanged, ElementName=cmbId}" Width="100"/>
                <DataGridTextColumn Header="Color" Binding="{Binding SelectedItem.Color, UpdateSourceTrigger=PropertyChanged, ElementName=cmbId}" Width="100"/>
            </DataGrid.Columns>
 </DataGrid>

How to display data "Auto Name & Color" value after user select item on ComboBox? 用户在ComboBox上选择项目后如何显示数据“自动名称和颜色”值?

The ItemsSource of a DataGrid is supposed to be set to an IEnumerable<T> . 应该将DataGridItemsSource设置为IEnumerable<T> If you only want to display a single item in the DataGrid , you could handle the SelectionChanged event for the ComboBox and set the ItemsSource property of the DataGrid to a List<T> that contains the selected item in the ComboBox : 如果只想在DataGrid显示单个项目,则可以处理ComboBoxSelectionChanged事件,并将DataGridItemsSource属性设置为List<T> ,该List<T>包含ComboBox中的选定项目:

private void cmbId_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    UGrid.ItemsSource = new List<YourEntityType> { cmbId.SelectedItem as YourEntityType };
}

<DataGrid x:Name="UGrid" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Auto Name" Binding="{Binding AutoName, UpdateSourceTrigger=PropertyChanged}" Width="100"/>
        <DataGridTextColumn Header="Color" Binding="{Binding Color, UpdateSourceTrigger=PropertyChanged}" Width="100"/>
    </DataGrid.Columns>
</DataGrid>

You need to add an event in combobox: 您需要在组合框中添加一个事件:

 > <ComboBox x:Name="cmbId" DisplayMemberPath="Id" > SelectionChanged="selectionInComboboxChanged"/> 

And in the selectionInComboboxChanged you can get the selected cobobox item and then add it to the mydata.test_views list. 然后在selectionInComboboxChanged中,可以获取选定的cobobox项,然后将其添加到mydata.test_views列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM