一.Combox
<ComboBox Name="cboStatus" Height="23" Width="103" Margin="2 10 10 10" SelectedIndex="0" BorderBrush="#93688693" Background="#FFECF5F5">
<ComboBoxItem Content="内容1 />
<ComboBoxItem Content="内容2" />
</ComboBox>
二.Combox显示图标
代码:
<ComboBox Height="33" HorizontalAlignment="Right" Margin="0,94,31,0" x:Name="comboBox1" VerticalAlignment="Top" Width="142" SelectedIndex="0">
<ComboBoxItem>
<StackPanel Orientation="Horizontal">
<Image Source="Images/roles.png" Height="30" />
<TextBlock Text="Select a role" />
</StackPanel>
</ComboBoxItem>
<ComboBoxItem Background="LightCoral">
<StackPanel Orientation="Horizontal">
<Image Source="Images/cashier.gif" Height="30" />
<TextBlock Text="Cashier" />
</StackPanel>
</ComboBoxItem>
<ComboBoxItem Background="LightGreen">
<StackPanel Orientation="Horizontal">
<Image Source="Images/manager.gif" Height="30" />
<TextBlock Text="Manager" />
</StackPanel>
</ComboBoxItem>
</ComboBox>
三.Combox数据的绑定
前台代码:
<ComboBox Height="23" HorizontalAlignment="Left" Margin="86,143,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120">
</ComboBox>
后台代码:
class ProductImg //声明类
{
int id;
public int Id
{
get { return id; }
set { id = value; }
}
string img;
public string Img
{
get { return img; }
set { img = value; }
}
}
ObservableCollection<ProductImg> imgs = new ObservableCollection<ProductImg>(); //集合,即数据源
comboBox1.SelectedValuePath = "Id"; //程序内部维护的值
comboBox1.DisplayMemberPath = "Img"; //显示的内容
comboBox1.ItemsSource = imgs; //数据源
comboBox1.SelectedValue = 3; //选中的值
四.Combox模板的使用
(1)Visibility="Collapsed" 可以隐藏当前子项
<ComboBox Height="23" Name="comboBox3" Width="103" Margin="15" Text="用?户§类 え?型í" SelectedIndex="0" BorderBrush="Black">
<ComboBoxItem IsSelected="False" Visibility="Collapsed">
<StackPanel Orientation="Horizontal">
<TextBlock Text="用?户§类え?型í" />
</StackPanel>
</ComboBoxItem>
</ComboBox>
(2)绑定数据
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Key}">
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
五.Combox获取选中值
获取选中对象:combox1.SelectedItem
获取选中索引值:combox1.SelectedIndex
获取选中的值:cboUsertype.SelectedValue