gpt4 book ai didi

c# - 如何用项目和一个文本框填充组合框

转载 作者:行者123 更新时间:2023-11-30 21:41:37 24 4
gpt4 key购买 nike

我有两个 ComboBoxes,根据第一个的选择,第二个被填充。

当第一个 Other 被选中时,如何使第二个 ComboBox 执行或显示 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 属性设置为包含 TextBoxDataTemplate > 当“其他”被选中时:

<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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com