开发者

Pattern for working with a form in Silverlight 4? (How to get references to XAML elements)

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="Cent开发者_开发知识库er"
                                               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?


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.:-)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜