帮助在 Silverlight 中使用可视化树

发布于 2024-10-13 12:45:06 字数 2209 浏览 9 评论 0原文

我有一个问题,如何使用可视树助手来获取我需要的对象。 我有一个名为 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 技术交流群。

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

发布评论

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

评论(1

沉溺在你眼里的海 2024-10-20 12:45:06

您应该尝试 Linq To VisualTree:

http:// /www.scottlogic.co.uk/blog/colin/2010/03/linq-to-visual-tree/

这允许您使用 Linq 风格的 API 查询可视化树(它实际上与 Linq 非常相似)到 XML)。在您的示例中,您想要查找特定类型的后代。您可以按如下方式执行此操作:

var dialogItemControl = button.Descendants<DialogItemControll>()
                              .Cast<DialogItemControll>()
                              .Single();

查询的第一部分查找给定类型的查询元素的所有后代(您还可以使用 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:

var dialogItemControl = button.Descendants<DialogItemControll>()
                              .Cast<DialogItemControll>()
                              .Single();

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.

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