在 AutoCompleteBox 的 Silverlight 4 工具包示例中,如何在 xaml 中切换下拉列表?
示例直接取自 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已将整行(以 Click=... 开头)替换为以下内容;
我现在不再需要事件处理程序方法。
I have replaced the whole line (starting with Click=...), with the following;
I have eliminated the need now for the event handler method.
您可以使用状态来实现此行为。在这种情况下,您将定义两种状态: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.