在wpf中,当我按下按钮时,它会关闭所有的堆栈面板,我需要在按下按钮时关闭特定的堆栈面板

utugiqy6  于 2023-04-13  发布在  其他
关注(0)|答案(1)|浏览(124)

正如我在标题中所说的,当关闭按钮被按下时,我需要关闭与stackpanel项目交互的按钮。但我是wpf新手,不知道如何将stackpanel或按钮与特定的stackpanel项目绑定。你能帮助我吗?

`<DataTemplate x:Key="fixTemplate">
    <Grid Background="Transparent" VerticalAlignment="Center" HorizontalAlignment="Center">
        <StackPanel  x:Name="fixPanel" MouseWheel="StackPanel_MouseWheel">
            <Border BorderThickness="1" CornerRadius="10" Opacity="1.0" Background="{dxi:ThemeResource {dxmt:MapBrushesThemeKey ResourceKey=OverlayBackground}}">
                <StackPanel Orientation="Vertical" Margin="8">
                    <Button x:Name="fixButton" Uid="0++" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StackPanel}}}" Content="X" Background="Red" Width="25" Height="25" HorizontalAlignment="Right" Click="fixButton_Click"/>
                    <TextBlock x:Name="fixText" Text="{Binding ToolTipText}" Foreground="{dxi:ThemeResource {dxmt:MapBrushesThemeKey ResourceKey=OverlayElementForeground}}"  TextWrapping="Wrap"/>
                </StackPanel>
            </Border>
            <Path Stretch="Fill" 
              Data="m 66.709675,141.93548 29.096774,-33 27.677421,33.35484 -18.80645,-0.35484 0.70968,43.29032 H 87.64516 l -0.709676,-43.29032 z"
              HorizontalAlignment="Center"
              Height="15"
              Width="20"
              UseLayoutRounding="False"
              VerticalAlignment="Center"
              Stroke="#BF000000" 
              StrokeThickness="0.4"
              Margin="-13 0 0 0" >
                <Path.Effect>
                    <DropShadowEffect ShadowDepth="2" BlurRadius="7" Opacity="0.5"/>
                </Path.Effect>
                <Path.Fill>
                    <RadialGradientBrush>
                        <GradientStop Color="Black" Offset="1." />
                    </RadialGradientBrush>
                </Path.Fill>
            </Path>
        </StackPanel>                
    </Grid>
</DataTemplate>`

正如你所看到的,我有一个按钮和动态创建的堆栈面板。我如何删除它的按钮被点击的堆栈面板?
我试图删除stackpanel,它是在它的删除按钮,但每个stackpanel被删除在我的代码。我不知道如何指定哪个stackpanel将删除时,该stackpanel的关闭按钮被按下

tpxzln5u

tpxzln5u1#

我尝试的代码是这样的

public IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
    {
        if (depObj != null)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(depObj, i);

                if (child != null && child is T)
                    yield return (T)child;

                foreach (T childOfChild in FindVisualChildren<T>(child))
                    yield return childOfChild;
            }
        }
    }

    private void fixButton_Click(object sender, RoutedEventArgs e)
    {
        string tooltip = "";
        List<string> ttlist = new List<string>();
        foreach (var textblock in FindVisualChildren<TextBlock>(this))
        {
            if (textblock.Name == "fixText")
            {
                tooltip = textblock.Text;
                ttlist.Add(tooltip);
            }
            break;
        }

        for (int i = 0; i < RealTimeMapDataGeneratorModel.sb.Count; i++)
        {
            System.Console.WriteLine(tooltip);
            System.Console.WriteLine(RealTimeMapDataGeneratorModel.sb[i].ToolTipText);
            if (ttlist[i] == RealTimeMapDataGeneratorModel.sb[i].ToolTipText)
            {
                //SubmarineVectorLayer.Visibility = Visibility.Hidden;
                RealTimeMapDataGeneratorModel.Submarine.RemoveAt(i);
                RealTimeMapDataGeneratorModel.sb.RemoveAt(i);
            }

        }
    }

相关问题