在 Silverlight 4 中使用表单的模式? (如何获取对XAML元素的引用)

发布于 2024-10-07 06:17:09 字数 3043 浏览 0 评论 0原文

我有一个表单:

                    <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>

此表单出现在 ItemsControlDataTemplate 中。我不确定单击按钮时从 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 技术交流群。

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

发布评论

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

评论(2

孤星 2024-10-14 06:17:09

您可以向演示者添加一个属性,例如 SelectedItemInAutoCompleteBox,然后使用 将其绑定到 AutoCompleteBoxSelectedItem 属性Mode=TwoWay,像这样,

<sdk:AutoCompleteBox SelectedItem="{Binding Path=DataContext.SelectedItemInAutoCompleteBox, Mode=TwoWay}"  ... />

您也可以尝试使用 AutoCompleteBoxText 属性来尝试相同的方法。看看它是否能解决您的问题。:-)

You can add a property, say SelectedItemInAutoCompleteBox, to your presenter, and then can bind it to the SelectedItem property of AutoCompleteBox, using Mode=TwoWay, like this,

<sdk:AutoCompleteBox SelectedItem="{Binding Path=DataContext.SelectedItemInAutoCompleteBox, Mode=TwoWay}"  ... />

You may try the same approach with Text property of AutoCompleteBox, also. See if it solves your problem.:-)

不顾 2024-10-14 06:17:09

您有多种选择:

  1. 如果您使用的是 Silverlight 5,请使用 AncestorBinding

  2. 否则,请使用 Silverlight 4 AncestorBinding hack(看起来不太漂亮)< /p>

  3. 或者你可以尝试DataContextProxy,它将 DataContext 存储在资源中以便可访问。注意:您应该将 DataContextProxy 设置为 topicList ListBox 的资源,而不是 Dan Wahlin 示例中的 UserControl。

You have several choices:

  1. If you're on Silverlight 5, use the AncestorBinding

  2. Otherwise, use a Silverlight 4 AncestorBinding hack (it doesn't look pretty)

  3. 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.

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