在 AutoCompleteBox 的 Silverlight 4 工具包示例中,如何在 xaml 中切换下拉列表?

发布于 2024-10-14 14:18:30 字数 1081 浏览 3 评论 0原文

示例直接取自 Silverlight 4 Toolkit - Samples 源代码。

我们有一个 AutoCompleteBox 的样式,使其像一个组合框:

<ControlTemplate TargetType="input:AutoCompleteBox">
              <Grid Margin="{TemplateBinding Padding}">
                ...
                Click="DropDownToggle_Click">

现在,在他们的示例中,他们在代码后面有一个单击事件处理程序(如下所列),但是我试图在 xaml 中定义此方法(ie我不需要文件后面的代码

private void DropDownToggle_Click(object sender, RoutedEventArgs e)
        {
            FrameworkElement fe = sender as FrameworkElement;
            AutoCompleteBox acb = null;
            while (fe != null && acb == null)
            {
                fe = VisualTreeHelper.GetParent(fe) as FrameworkElement;
                acb = fe as AutoCompleteBox;
            }
            if (acb != null)
            {
                if (string.IsNullOrEmpty(acb.SearchText))
                {
                    acb.Text = string.Empty;
                }
                acb.IsDropDownOpen = !acb.IsDropDownOpen;
            }
        }

这可能吗?

Sample taken straight from the Silverlight 4 Toolkit - Samples source code.

We have a style for the AutoCompleteBox to make it like a combobox :

<ControlTemplate TargetType="input:AutoCompleteBox">
              <Grid Margin="{TemplateBinding Padding}">
                ...
                Click="DropDownToggle_Click">

Now, in their sample, they have a click event handler in code behind (listed below), however I was trying to define this method in the xaml ( i.e. I don't want a code behind file )

private void DropDownToggle_Click(object sender, RoutedEventArgs e)
        {
            FrameworkElement fe = sender as FrameworkElement;
            AutoCompleteBox acb = null;
            while (fe != null && acb == null)
            {
                fe = VisualTreeHelper.GetParent(fe) as FrameworkElement;
                acb = fe as AutoCompleteBox;
            }
            if (acb != null)
            {
                if (string.IsNullOrEmpty(acb.SearchText))
                {
                    acb.Text = string.Empty;
                }
                acb.IsDropDownOpen = !acb.IsDropDownOpen;
            }
        }

Is this possible ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧街凉风 2024-10-21 14:18:30

我已将整行(以 Click=... 开头)替换为以下内容;

IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsDropDownOpen, Mode=TwoWay}" 

我现在不再需要事件处理程序方法。

I have replaced the whole line (starting with Click=...), with the following;

IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsDropDownOpen, Mode=TwoWay}" 

I have eliminated the need now for the event handler method.

一袭白衣梦中忆 2024-10-21 14:18:30

您可以使用状态来实现此行为。在这种情况下,您将定义两种状态:PopupOpen 和 PopupClosed。 PopupOpen 状态将 IsDropDownOpen 属性设置为 True,将 PopupClosed 设置为 false。
然后添加一个 DataStateBehaviour 并将其绑定到切换按钮的 IsChecked 属性。 IsChecked = true = 打开,IsChecked = false = 关闭

我从未尝试过此操作,但这应该可行。

您使用混合吗?

在这里您可以找到有关 DataStateBehaviour 的一些信息:
http://msdn.microsoft.com/en-我们/库/ff723952(v=表达式.40).aspx
http://msdn.microsoft.com/de-de/library/microsoft.expression.interactivity.core.datastatebehavior_members(v=expression.40).aspx
http://silverlightforbusiness.net/ 2010/04/26/using-the-datastatebehavior-for-loading-animations-in-mvvm/

希望这会有所帮助。

BR、

TJ

编辑:尝试将 IsOpen 属性直接绑定到切换按钮的 IsChecked 状态。这也应该有效。

You could use states for this behaviour. In this case you would define two states: PopupOpen and PopupClosed. The PopupOpen State sets the IsDropDownOpen Property to True and the PopupClosed to false.
Then you add an DataStateBehaviour and bind it to the IsChecked Property of the toggle button. IsChecked = true = Open, IsChecked = false = Closed

I never tried this, but this should work.

Are you using Blend?

Here you can find some information about the DataStateBehaviour:
http://msdn.microsoft.com/en-us/library/ff723952(v=expression.40).aspx
http://msdn.microsoft.com/de-de/library/microsoft.expression.interactivity.core.datastatebehavior_members(v=expression.40).aspx
http://silverlightforbusiness.net/2010/04/26/using-the-datastatebehavior-for-loading-animations-in-mvvm/

Hope this helps.

BR,

TJ

EDIT: Try binding the IsOpen Property directly to the IsChecked State of the toggle Button. This should work, too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文