在WPF中自动打开下拉列表 [英] Automatically opening a drop down in WPF

查看:67
本文介绍了在WPF中自动打开下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ComboBox,我想在某些情况下自动打开下拉元素。



ComboBox定义如下:



 <   ComboBox     x:名称  = < span class =code-keyword> WorkTypeTemplates   保证金  =  5,0,5,0  

< span class =code-attribute> 前景 = {StaticResource SETISelection} ItemsSource = {Binding}

Horizo​​ntalAlignment < span class =code-keyword> = 拉伸 VerticalAlignment = 拉伸

< span class =code-attribute> SelectionChanged = TemplateChanged >
< ComboBox.ItemTemplate >
< DataTemplate >
< StackPanel < span class =code-attribute> Orientation = 水平 >
< TextBlock 文本 = {Binding HeaderLabel} / >
< / StackPanel >
< / DataTemplate >
< / ComboBox.ItemTemplate >
< / ComboBox >





...根据列表内容在代码中设置IsDropDown属性。



当IsDropDown设置为True时,当窗体打开时,弹出窗口将打开然后再次关闭(使用Snoop检查,IsDropDown已重置为错误)。



我尝试使用Loaded事件在各个点设置/重置属性形式和控制,但我尝试过的所有内容都会导致相同的结果。



我确定我错过了一些令人眼花缭乱的东西(XAML)一般来说,前端真的不是我的专业主题)但这对我来说是个便利,因为它应该是一个非常微不足道的变化。任何帮助将不胜感激。

解决方案

我不能让它失败...



我创建了一个简单的WPF应用程序,它有一个ComboBox和一个Button。 Window的构造函数将IsDropDownOpen属性设置为true。然后按钮也会这样做。



你遗漏了你用什么标准确定这是否属实的细节,但你应该能够在那里指定它,或者你可以在XAML中使用触发器。



以下代码显示我的演示应用程序:



XAML(为简洁而省略)

< ComboBox x:Name =MyComboBoxMargin =5 0 5 0Grid.Column =0
ItemsSource ={Binding Path =。}>
< ComboBox.ItemTemplate>
< DataTemplate>
< TextBlock Text ={Binding Path =。}/>
< / DataTemplate>
< /ComboBox.ItemTemplate>
< / ComboBox>
< Button Grid.Column =2Margin =0 0 5 0
Content =Open DropDown
Click =Button_Click/>





代码隐藏(为简洁而省略)

  readonly 列表< string> items =  new 列表< string>(){  1  两个 }; 

public MainWindow()
{
InitializeComponent();
DataContext = items;

MyComboBox.IsDropDownOpen = true ;
}

private void Button_Click( object sender,RoutedEventArgs e)
{
MyComboBox.IsDropDownOpen = true ;
} < / string > < / string >


I have a ComboBox with which I would like to automatically open the drop-down element in certain situations.

The ComboBox is defined as follows:

<ComboBox x:Name="WorkTypeTemplates" Margin="5,0,5,0"

                    Foreground="{StaticResource SETISelection}" ItemsSource="{Binding}" 

                    HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 

                    SelectionChanged="TemplateChanged" >
            <ComboBox.ItemTemplate>
              <DataTemplate>
                <StackPanel Orientation="Horizontal">
                  <TextBlock Text="{Binding HeaderLabel}" />
                </StackPanel>
              </DataTemplate>
            </ComboBox.ItemTemplate>
          </ComboBox>



... with the IsDropDown property being set in code according to the contents of the list.

When IsDropDown set to "True", as the form opens the pop-up will open and then close again (checking with Snoop, IsDropDown has been reset to "False").

I've tried setting/resetting the property at various points using the Loaded events on both the form and the control but everything that I've tried has lead to the same result.

I'm sure that I'm missing something blindingly obvious (XAML and front-ends in general really aren't my specialist subject) but this is driving me potty as it's something that should be a very trivial change. Any help would be greatly appreciated.

解决方案

I cannot get it to fail...

I created a simple WPF application that has a ComboBox and a Button. The constructor of the Window sets the IsDropDownOpen property to true. And then the Button will do the same.

You left out the details of what criteria you use to determine if that is true or not, but you should be able to specify it there, or you can use triggers within the XAML.

The following code shows my demo application:

XAML (some elided for brevity)

<ComboBox x:Name="MyComboBox" Margin="5 0 5 0" Grid.Column="0"
            ItemsSource="{Binding Path=.}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=.}" />
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
<Button Grid.Column="2" Margin="0 0 5 0"
        Content="Open DropDown"
        Click="Button_Click" />



Code-behind (some elided for brevity)

readonly List<string> items = new List<string>() { "One", "Two", "Three" };

public MainWindow()
{
    InitializeComponent();
    DataContext = items;

    MyComboBox.IsDropDownOpen = true;
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    MyComboBox.IsDropDownOpen = true;
}</string></string>


这篇关于在WPF中自动打开下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆