简体   繁体   English

如何绑定Observablecollection <T> 在wpf中使用组合框

[英]How to bind Observablecollection<T> with combobox in wpf

I am trying to bind Observablecollection<T> with ComboBox . 我试图将Observablecollection<T>ComboBox绑定。 ComboBox having Datatemplete 具有Datatemplete ComboBox

<ComboBox Width="150" Margin="20,0,0,5" Name="cbSelection" Height="20" 
              BorderThickness="2" BorderBrush="Black" 
              SelectedIndex="0" DataContext="{Binding AdComboBox}">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="1*"/>
                                <ColumnDefinition Width="1*"/>
                                <ColumnDefinition Width="1*"/>
                            </Grid.ColumnDefinitions>
                            <TextBlock Grid.Column="0" Text="{Binding XPath=LOC, Mode=OneWay}" Margin="5,0,5,0"/>
                            <TextBlock Grid.Column="1" Text="{Binding XPath=PUB, Mode=OneWay}" Margin="0,0,5,0"/>
                            <TextBlock Grid.Column="2" Text="{Binding XPath=EDI, Mode=OneWay}" Margin="0,0,5,0"/>
                        </Grid>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>

but not get the data in ComboBox what going wrong thanks in advance 但没有得到ComboBox的数据ComboBox了,谢谢

Use the ItemsSource of the ComboBox to point to the ObservableCollection<T> . 使用ComboBox的ItemsSource指向ObservableCollection<T> Also: Use Path, not XPath which is used for binding to XML documents. 另外:使用路径,而不是用于绑定到XML文档的XPath。

<ComboBox Width="150" Margin="20,0,0,5" Name="cbSelection" Height="20" 
          BorderThickness="2" BorderBrush="Black" 
          ItemsSource="{Binding AdComboBox}"
          SelectedIndex="0">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="1*"/>
                    <ColumnDefinition Width="1*"/>
                    <ColumnDefinition Width="1*"/>
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" 
                           Text="{Binding Path=LOC, Mode=OneWay}"
                           Margin="5,0,5,0"/>
                <TextBlock Grid.Column="1" 
                           Text="{Binding Path=PUB, Mode=OneWay}"
                           Margin="0,0,5,0"/>
                <TextBlock Grid.Column="2" 
                           Text="{Binding Path=EDI, Mode=OneWay}"
                           Margin="0,0,5,0"/>
            </Grid>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

Side note: you might want to rename the collection to something more functional instead of AdComboBox . 旁注:您可能希望将集合重命名为更具功能性的名称,而不是AdComboBox Eg, Ads Because it is not a ComboBox but it is a collection of Ads(?) 例如, 广告,因为它不是ComboBox,但它是Ads(?)的集合

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

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