帮助在 Silverlight 中使用可视化树
我有一个问题,如何使用可视树助手来获取我需要的对象。 我有一个名为 DialogItemControll 的用户控件,我从主页调用它,如下所示:
DialogItemControll ivDialogWindow = new DialogItemControll()
ivDialogWindow.ivSave.Click += new RoutedEventHandler(ivSave_Click);
ivDialogWindow.Show();
然后,我有一个方法 ivSave_Click,当我单击用户控件上的保存按钮时,该方法会被调用。该方法如下所示:
void ivSave_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button;
var firstStack = button.Parent as StackPanel;
var secondStack = firstStack.Parent as StackPanel;
TextBox te = secondStack.FindName("ivUserComment") as TextBox;}
您可以在此处看到我尝试使用 getparent 等。做得不太好。所以我想要的是得到整个对象,如下所示:
var controll = ?? as DialogItemControll
我的 DialogItemControll 看起来像这样:
<C1:C1Window x:Class="DialogItemControll"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:C1="clr-namespace:C1.Silverlight;assembly=C1.Silverlight"
mc:Ignorable="d"
d:DesignHeight="418" d:DesignWidth="401">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Margin="5,5,5,5">
<TextBlock Name="ivHelpComment" FontSize="18">test</TextBlock>
<TextBox Name="ivUserComment" BorderThickness="2,2,2,2" Height="170"></TextBox>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" >
<Button Name="ivSave" HorizontalAlignment="Right" Height="22" Width="70" Margin="0,10,20,0" Click="ivSave_Click">Spara</Button>
<Button Name="ivCancel" HorizontalAlignment="Right" Height="22" Width="70" Margin="0,10,20,0" Click="ivCancel_Click">Avbryt</Button>
</StackPanel>
</StackPanel>
</Grid>
C1窗口与用户控件相同,只是第三方控件。 请帮助我如何以良好的方式使用树助手来获取整个对象。
谢谢
I have a question of how I can use the visual tree helper to get an object that I need.
I have a user controll called DialogItemControll that I call from my main page like this:
DialogItemControll ivDialogWindow = new DialogItemControll()
ivDialogWindow.ivSave.Click += new RoutedEventHandler(ivSave_Click);
ivDialogWindow.Show();
And then I have the method ivSave_Click that gets called when I click the save button on my user controll. That method looks like:
void ivSave_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button;
var firstStack = button.Parent as StackPanel;
var secondStack = firstStack.Parent as StackPanel;
TextBox te = secondStack.FindName("ivUserComment") as TextBox;}
This is where you can see my attempts to use the get parent and so on. Not so nicely done. So what I want is to get the whole object like:
var controll = ?? as DialogItemControll
My DialogItemControll looks like this :
<C1:C1Window x:Class="DialogItemControll"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:C1="clr-namespace:C1.Silverlight;assembly=C1.Silverlight"
mc:Ignorable="d"
d:DesignHeight="418" d:DesignWidth="401">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Margin="5,5,5,5">
<TextBlock Name="ivHelpComment" FontSize="18">test</TextBlock>
<TextBox Name="ivUserComment" BorderThickness="2,2,2,2" Height="170"></TextBox>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" >
<Button Name="ivSave" HorizontalAlignment="Right" Height="22" Width="70" Margin="0,10,20,0" Click="ivSave_Click">Spara</Button>
<Button Name="ivCancel" HorizontalAlignment="Right" Height="22" Width="70" Margin="0,10,20,0" Click="ivCancel_Click">Avbryt</Button>
</StackPanel>
</StackPanel>
</Grid>
C1 window is the same as user control, its just a third party control.
Please help me with how I should use the tree helper in a good way to get the whole object.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该尝试 Linq To VisualTree:
http:// /www.scottlogic.co.uk/blog/colin/2010/03/linq-to-visual-tree/
这允许您使用 Linq 风格的 API 查询可视化树(它实际上与 Linq 非常相似)到 XML)。在您的示例中,您想要查找特定类型的后代。您可以按如下方式执行此操作:
查询的第一部分查找给定类型的查询元素的所有后代(您还可以使用 Linq to VisualTree 查找子项、同级等...),第二部分转换返回的列表到给定类型,最后因为我们知道给定类型只有一个后代,所以我们可以使用 Single() 来提取单个项目。
希望有帮助。
You should try Linq To VisualTree:
http://www.scottlogic.co.uk/blog/colin/2010/03/linq-to-visual-tree/
This allows you to query the visual tree using a Linq style API (It is actually very similar to Linq to XML). In your example, you want to find a Descendant of a specific type. You can do this as follows:
The first part of the query finds all descendants of the queried element of a given type (you can also use Linq to VisualTree to find children, peers etc...), the second part casts the returned list to the given type, finally because we know there is only one descendant of the given type, we can use Single() to extract the single item.
Hope that helps.