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

标签 c# wpf combobox

我有两个组合框,根据第一个的选择,填充秒。

当在第一个组合框中选择其他时,如何使第二个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 属性设置为包含 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/

相关文章:

c# - 在运行时更改 App.config

c# - 基于组合框值,更改刷新计时器

wpf - 删除组合框所选项目文本突出显示

javascript - 组合下拉列表不使用 javascript jquery getJSON 从文本文件生成数据

c# - 我如何从框架中获取页面实例?

c# - Master/Detail MVVM 中的 Wpf Combobox

c# - 系统找不到 Windows Server 2012 中指定的文件

c# - 使用 FileSystemWatcher 触发事件,然后删除新创建的文件?

c# - 查询字符串值始终为 0

c# - 使用附加属性在 XAML 中为 WPF 元素设置动画?