1. All Controls
All Controls Offcail Website: learn.microsoft.com/zh-cn/dotne…
也可以參考下面
Initializing2. ComboBox
learn.microsoft.com/zh-cn/dotne…
| name | Value |
|---|---|
| IsEditable | 获取或设置一个值,该值指示启用或禁用 ComboBox 的文本框中的文本编辑。 |
| IsEnabled | 获取或设置一个值,该值指示是否 用户界面 (UI) 中启用此元素。 这是依赖项属性。(继承自 UIElement) |
| SelectedIndex | 获取或设置当前选择中第一项的索引,如果选择为空,则返回负一(-1)。(继承自 Selector) |
| SelectedItem | 获取或设置当前选择中的第一项,或者,如果选择为空,则返回 null。(继承自 Selector) |
| SelectedValue | 获获取或设置通过使用 SelectedItem 而获取的 SelectedValuePath 的值。(继承自 Selector) |
| SelectedValuePath | 获取或设置用于从 SelectedValue 获取 SelectedItem 的路径。(继承自 Selector) |
| Text | 获取或设置当前选定项的文本。 |
2.1 xaml
此處使用的是Prism的Mvvm模式和MaterialDesign樣式
<UserControl x:Class="BlankApp1.Views.Practice.ComboBoxView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:BlankApp1.Views.Practice"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:prism="http://prismlibrary.com/"
mc:Ignorable="d"
d:DesignHeight="450"
d:DesignWidth="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<DockPanel LastChildFill="False">
<ComboBox x:Name="PalletModeComboBox"
Margin="30,0,0,0"
Width="150"
Height="50"
VerticalAlignment="Center"
SelectedValue="{Binding SelectedModeCode,Mode=TwoWay}"
SelectedValuePath="Content"
HorizontalAlignment="Left"
SelectedIndex="{Binding ComboBoxIndex,Mode=TwoWay}"
Style="{StaticResource MaterialDesignOutlinedComboBox}">
<ComboBoxItem Content="model"
Tag="1"></ComboBoxItem>
<ComboBoxItem Content="mode2"
Tag="2"></ComboBoxItem>
<ComboBoxItem Content="mode3"
Tag="3"></ComboBoxItem>
<ComboBoxItem Content="mode4"
Tag="4"></ComboBoxItem>
<ComboBoxItem Content="mode5"
Tag="5"></ComboBoxItem>
<ComboBoxItem Content="mode6"
Tag="6"></ComboBoxItem>
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<prism:InvokeCommandAction Command="{Binding ModeChangedCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
<Button DockPanel.Dock="Right"
Command="{Binding ShowMsg}"
Margin="3">
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="AlertDecagram"
Margin="3" />
<TextBlock Text="確定"
Margin="3" />
</StackPanel>
</Button>
<Button DockPanel.Dock="Right"
Command="{Binding ShowMsg2}"
Margin="3">
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="AlertDecagram"
Margin="3" />
<TextBlock Text="取消"
Margin="3" />
</StackPanel>
</Button>
</DockPanel>
</Grid>
</UserControl>
2.2 Code
如果想設定初始值,直接給index賦值就好了。
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BlankApp1.ViewModels.Practice
{
public class ComboBoxViewModel : BindableBase
{
/// <summary>
/// 下拉的選值
/// </summary>
private string selectedModeCode;
public string SelectedModeCode
{
get => selectedModeCode;
set => SetProperty(ref selectedModeCode, value);
}
/// <summary>
/// 下拉的索引
/// </summary>
private int comboBoxIndex;
public int ComboBoxIndex
{
get { return comboBoxIndex; }
set { comboBoxIndex = value; }
}
public DelegateCommand ModeChangedCommand { get; private set; }
public DelegateCommand ShowMsg { get; private set; }
public DelegateCommand ShowMsg2 { get; private set; }
public ComboBoxViewModel()
{
ComboBoxIndex = 0;
ModeChangedCommand = new DelegateCommand(ModeCommand);
ShowMsg = new DelegateCommand(() => {
System.Diagnostics.Trace.WriteLine("---------ComboBox Select Value : " + SelectedModeCode);
System.Diagnostics.Trace.WriteLine("---------ComboBox Select Index : " + ComboBoxIndex);
});
ShowMsg2 = new DelegateCommand(() =>
{
System.Diagnostics.Trace.WriteLine("this is a cancel button");
});
}
private void ModeCommand()
{
System.Diagnostics.Trace.WriteLine("---------ComboBox Select Value : " + SelectedModeCode);
System.Diagnostics.Trace.WriteLine("---------ComboBox Select Index : " + ComboBoxIndex);
}
}
}