简体   繁体   中英

Can't Use combobox in C# code

I want to add itens to a combobox but in c# because i dont want to add all the time the same comboboxitens. My code is this to the combobox i want to add itens.

    <ScrollViewer Margin="252,130,296,134" Grid.Row="1" VerticalAlignment="Top" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollMode="Enabled" ScrollViewer.HorizontalScrollMode="Disabled" ScrollViewer.ZoomMode="Disabled">
            <StackPanel >
                <ItemsControl x:Name="ic" Grid.Row="2">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Grid HorizontalAlignment="Left" VerticalAlignment="Center">
                                <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
                                    <Border BorderBrush="#C83245" Background="White"  BorderThickness="1">
                                        <ComboBox x:Name="cbSeletion" VerticalAlignment="Center" FontSize="14" Width="250" Height="40" Foreground="Black" Tapped="cbSeletion_Tapped">

                                        </ComboBox>
                                    </Border>
                                    <Border BorderBrush="#C83245" Background="White" BorderThickness="1">
                                        <TextBlock Text="{Binding Name}" FontSize="14" VerticalAlignment="Center" Width="350" Foreground="Black"/>
                                    </Border>
                                    <Border BorderBrush="#C83245" Background="White"  BorderThickness="1" >
                                        <TextBlock Text="{Binding Position}" FontSize="14" VerticalAlignment="Center" Width="250" Foreground="Black"/>
                                    </Border>
                                </StackPanel>
                            </Grid>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </StackPanel>
        </ScrollViewer>

what can i do to use it?

You can bind the combobox ItemsSource to an ObservableCollection in your viewmodel:

<ComboBox ItemsSource="{Binding Items}"/>

The property:

ObservableCollection<string> _items = new ObservableCollection<string>();
public ObservableCollection<string> Items
{
     get
     {
          return _items;
     }
}

You can then add items to and from that collection and the items in the dropdown will reflect it.

If you have really few items, you could directly use the ComboBoxItem in XAML, but still it is not that ellegant. The MVVM approach is definately the answer, especially for many items in the collection of the ComboBox, cause it gives you greater flexibility and the logic is not that hard-coded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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