我有两个组合框
,根据第一个的选择,填充秒。
当在第一个组合框中选择其他时,如何使第二个ComboBox
Action 或显示TextBox
,以便用户可以输入数字/字母.
C#
public class MyViewModel
{
public ICommand UpdateCommand { get; private set; }
private ObservableCollection<string> _Veh = new ObservableCollection<string>();
private ObservableCollection<string> _VehTypes = new ObservableCollection<string>();
private ObservableCollection<string> _cars = new ObservableCollection<string>();
private ObservableCollection<string> _planes = new ObservableCollection<string>();
public ObservableCollection<string> Veh
{
get { return _Veh; }
set { SetProperty(ref _Veh, value); }
}
public ObservableCollection<string> VehTypes
{
get { return _VehTypes; }
set { SetProperty(ref _VehTypes, value); }
}
public MyViewModel()
{
UpdateCommand = new DelegateCommand<object>(OnUpdate);
_Veh.Add("Cars");
_Veh.Add("Planes");
_Veh.Add("Other");
_Cars.Add("GM");
_Cars.Add("BMW");
_Cars.Add("Toyota");
_Cars.Add("Honda");
_planes.Add("AirBus");
_planes.Add("Boing");
}
private void OnUpdate(object myobj)
{
_VehTypes.Clear();
// Add new vehTypes based on _cars and _planes
}
}
wpf
<ComboBox Name="ComboVeh" ItemsSource="{Binding Veh}" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding UpdateCommand}"
CommandParameter="{Binding SelectedValue, ElementName=ComboVeh}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
<ComboBox Name="ComboVehTypes" ItemsSource="{Binding VehTypes}"/>
最佳答案
您可以使用带有触发器的 ContentControl
,该触发器将 ContentTemplate
属性设置为包含 TextBox
的 DataTemplate
> 当选择“其他”时:
<ComboBox Name="ComboVeh" ItemsSource="{Binding Veh}" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding UpdateCommand}"
CommandParameter="{Binding SelectedValue, ElementName=ComboVeh}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
<ContentControl Content="{Binding SelectedValue, ElementName=ComboVeh}">
<ContentControl.Style>
<Style TargetType="ContentControl">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<ComboBox Name="ComboVehTypes" ItemsSource="{Binding DataContext.VehTypes, RelativeSource={RelativeSource AncestorType=ContentControl}}"/>
</DataTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Content" Value="Other">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBox />
</DataTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
关于c# - 如何使用项目和一个文本框填充组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43126003/