在 Silverlight 4 中使用表单的模式? (如何获取对XAML元素的引用)
我有一个表单:
<StackPanel Orientation="Horizontal" Visibility="{Binding Editable, Converter={StaticResource visibilityConverter}}"
ToolTipService.ToolTip="Add new topic to this group">
<sdk:AutoCompleteBox Width="160" ItemsSource="{Binding ElementName=LayoutRoot, Path=DataContext.TopicNames}" />
<Button Click="addTopicButton_Click">
<Image Source="Images/appbar.add.rest.png" />
</Button>
</StackPanel>
此表单出现在 ItemsControl
的 DataTemplate
中。我不确定单击按钮时从 AutoCompleteBox 获取数据的最佳方法是什么。我无法赋予元素 x:Name
属性,因为它们位于模板中(对吗?)。
我该如何解决这个问题? Click
事件将为我提供 Button
,但我需要对文本框的引用。使用按钮的父级,然后在子级中查找文本框?如果我将其分解到它自己的 UserControl
中,我可以设置 x:Name
值,但我宁愿不这样做。
还有其他想法吗?
更新:这是此类问题的另一个示例:
<ListBox x:Name="topicList"
ItemsSource="{Binding Id, Converter={StaticResource topicGroupIDConverter}}"
SelectionChanged="ListBox_SelectionChanged"
HorizontalAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"
Width="150"
VerticalAlignment="Center"
ToolTipService.ToolTip="{Binding Description}"
ToolTipService.Placement="Right" />
<Button ToolTipService.ToolTip="Remove this topic from this group"
Visibility="{Binding ElementName=topicList,
Path=DataContext.Editable,
Converter={StaticResource visibilityConverter}}"
Click="removeTopicButton_Click"
HorizontalAlignment="Right"
Margin="10,0">
<Image Source="Images/appbar.cancel.rest.png" />
</Button>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
单击按钮时,我想访问topicList.DataContext
。但是,topicList
本身是 ItemsControl
中的 DataTemplate
,因此我无法使用代码隐藏中的名称来访问它。我还能怎样做呢?
I have a form:
<StackPanel Orientation="Horizontal" Visibility="{Binding Editable, Converter={StaticResource visibilityConverter}}"
ToolTipService.ToolTip="Add new topic to this group">
<sdk:AutoCompleteBox Width="160" ItemsSource="{Binding ElementName=LayoutRoot, Path=DataContext.TopicNames}" />
<Button Click="addTopicButton_Click">
<Image Source="Images/appbar.add.rest.png" />
</Button>
</StackPanel>
This form appears in a DataTemplate
for an ItemsControl
. I'm not sure what the best way is to get the data from the AutoCompleteBox
when the button is clicked. I can't give the elements x:Name
attributes, because they're in a template (right?).
How can I get around this? The Click
event will give me the Button
, but I need a reference to the text box. Use the Button's parent, then look through the children for the Textbox? If I factored this out into its own UserControl
, I could set x:Name
values, but I'd rather not do that.
Any other ideas?
Update: Here is another example of such a problem:
<ListBox x:Name="topicList"
ItemsSource="{Binding Id, Converter={StaticResource topicGroupIDConverter}}"
SelectionChanged="ListBox_SelectionChanged"
HorizontalAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"
Width="150"
VerticalAlignment="Center"
ToolTipService.ToolTip="{Binding Description}"
ToolTipService.Placement="Right" />
<Button ToolTipService.ToolTip="Remove this topic from this group"
Visibility="{Binding ElementName=topicList,
Path=DataContext.Editable,
Converter={StaticResource visibilityConverter}}"
Click="removeTopicButton_Click"
HorizontalAlignment="Right"
Margin="10,0">
<Image Source="Images/appbar.cancel.rest.png" />
</Button>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
When the button is clicked, I want to access topicList.DataContext
. However, topicList
itself is a DataTemplate
in an ItemsControl
, so I can't access it using its name from code-behind. How else can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以向演示者添加一个属性,例如
SelectedItemInAutoCompleteBox
,然后使用将其绑定到
,像这样,AutoCompleteBox
的SelectedItem
属性Mode=TwoWay您也可以尝试使用
AutoCompleteBox
的Text
属性来尝试相同的方法。看看它是否能解决您的问题。:-)You can add a property, say
SelectedItemInAutoCompleteBox
, to your presenter, and then can bind it to theSelectedItem
property ofAutoCompleteBox
, usingMode=TwoWay
, like this,You may try the same approach with
Text
property ofAutoCompleteBox
, also. See if it solves your problem.:-)您有多种选择:
如果您使用的是 Silverlight 5,请使用 AncestorBinding
否则,请使用 Silverlight 4 AncestorBinding hack(看起来不太漂亮)< /p>
或者你可以尝试DataContextProxy,它将 DataContext 存储在资源中以便可访问。注意:您应该将 DataContextProxy 设置为 topicList ListBox 的资源,而不是 Dan Wahlin 示例中的 UserControl。
You have several choices:
If you're on Silverlight 5, use the AncestorBinding
Otherwise, use a Silverlight 4 AncestorBinding hack (it doesn't look pretty)
Or you could try DataContextProxy, which stores the DataContext in a resource so that it is accessible. Note: you should set the DataContextProxy as a Resource of topicList ListBox, not the UserControl as in Dan Wahlin's example.