Wpf中的列表控件

1. ListBox

<Window x:Class="项目4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:项目4"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" SizeToContent="Height" WindowStartupLocation="CenterScreen" MinWidth="450" MinHeight="250">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <ListBox Margin="25" x:Name="ListBox1" Height="auto" SelectionChanged="ListBox1_SelectionChanged">
            <StackPanel Orientation="Horizontal">
                <ListBoxItem Margin="3">Green</ListBoxItem>
                <ListBoxItem Margin="3">Red</ListBoxItem>
                <ListBoxItem Margin="3">Yellow</ListBoxItem>
                <ListBoxItem Margin="3">Blue</ListBoxItem>
                <ListBoxItem Margin="3">
                    <Image Source="Images/open.ico" Height="35"></Image>
                </ListBoxItem>
                <StackPanel Margin="3">
                    <Button Padding="15,3">确认</Button>
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Margin="5" HorizontalAlignment="Center">
                        今天天气不错!
                    </TextBlock>
                </StackPanel>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
               <TextBlock>
                   <CheckBox>Option1</CheckBox>
                   <CheckBox>Option2</CheckBox>
                   <CheckBox>Option3</CheckBox>
                   <CheckBox>Option4</CheckBox>
               </TextBlock>
            </StackPanel>
        </ListBox>
        <StackPanel Grid.Row="1" Height="auto">
            <TextBlock x:Name="TextBlock1" Padding="25,5">Current selection</TextBlock>
            <TextBlock x:Name="TextBlock2" TextWrapping="Wrap" Height="120"></TextBlock>
            <Button x:Name="Button1" Margin="25" HorizontalAlignment="Center" Padding="15,3">Examine All Items</Button>
        </StackPanel>
    </Grid>
</Window>
private void ListBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (ListBox1.SelectedItem==null)
    {
        return;
    }

    TextBlock2.Text = $"You chose item at postion {ListBox1.SelectedIndex+1}.\nChecked state is {ListBox1.SelectedItems}";
}

c3d.club

2. ComboBox

<Grid>
    <ComboBox Margin="25" VerticalAlignment="Top" SelectedIndex="0">
        <ComboBoxItem>1</ComboBoxItem>
        <ComboBoxItem>2</ComboBoxItem>
        <ComboBoxItem>3</ComboBoxItem>
        <ComboBoxItem>4</ComboBoxItem>
    </ComboBox>
</Grid>

SelectedIndex="0"用来指定默认选中的项

c3d.club