In wpf, when I press button it closes all stackpanels. I need to close the specific stackpanel when button is pressed

huangapple go评论80阅读模式
英文:

In wpf, when I press button it closes all stackpanels. I need to close the specific stackpanel when button is pressed

问题

在标题中所说的,当按下关闭按钮时,我需要关闭与按钮交互的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>

正如你所看到的,我有一个按钮和动态创建的stackpanels。如何删除按下按钮的stackpanel?

我尝试删除位于其删除按钮下方的stackpanel,但是在我的代码中删除了每个stackpanel。我不知道如何指定按下该stackpanel的关闭按钮时将删除哪个stackpanel。

英文:

As I said in title, I need to close the button interacted stackpanel item when close button is pressed. But I'm new in wpf and can't understand how to bind stackpanel or button with specific stackpanel item. Can you please help me ?

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

As you see I have a button and dynamically created stackpanels. How can I delete the stackpanel that it's button is clicked ?

I tried to delete stackpanel which is under it's delete button but every stackpanel is deleted in my code. I don't know how to specify which stackpanel will deleted when that stackpanel's close button is pressed

答案1

得分: 0

这是我尝试的代码后面的部分:

    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);
            }
        }
    }
英文:

Code behind for what i tried is this

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

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

                foreach (T childOfChild in FindVisualChildren&lt;T&gt;(child))
                    yield return childOfChild;
            }
        }
    }

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

        for (int i = 0; i &lt; 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);
            }

        }
    }

答案2

得分: 0

我通过在堆栈面板中添加复选框来解决了这个问题

<Border BorderThickness="1" CornerRadius="10" Opacity="1.0" Background="{dxi:ThemeResource {dxmt:MapBrushesThemeKey ResourceKey=OverlayBackground}}">
    <StackPanel Orientation="Vertical" Margin="8">
        <CheckBox x:Name="MyCheckBox" IsChecked="True" HorizontalAlignment="Left"/>
        <TextBlock Text="{Binding ToolTipText}" Visibility="{Binding IsChecked, ElementName=MyCheckBox, Converter={dxmvvm:BooleanToVisibilityConverter}}" Foreground="{dxi:ThemeResource {dxmt:MapBrushesThemeKey ResourceKey=OverlayElementForeground}}" TextWrapping="Wrap"/>
    </StackPanel>
</Border>

在模型视图中的代码

public class BoolToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var isChecked = (bool)value;
        return isChecked ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
英文:

I solved this by adding checkbox to stack panel

&lt;Border BorderThickness=&quot;1&quot; CornerRadius=&quot;10&quot; Opacity=&quot;1.0&quot; Background=&quot;{dxi:ThemeResource {dxmt:MapBrushesThemeKey ResourceKey=OverlayBackground}}&quot;&gt;
   &lt;StackPanel Orientation=&quot;Vertical&quot; Margin=&quot;8&quot;&gt;
       &lt;CheckBox x:Name=&quot;MyCheckBox&quot; IsChecked=&quot;True&quot; HorizontalAlignment=&quot;Left&quot;/&gt;
       &lt;TextBlock Text=&quot;{Binding ToolTipText}&quot; Visibility=&quot;{Binding IsChecked, ElementName=MyCheckBox, Converter={dxmvvm:BooleanToVisibilityConverter}}&quot; Foreground=&quot;{dxi:ThemeResource {dxmt:MapBrushesThemeKey ResourceKey=OverlayElementForeground}}&quot;  TextWrapping=&quot;Wrap&quot;/&gt;
   &lt;/StackPanel&gt;
&lt;/Border&gt;

and this code in model view

public class BoolToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var isChecked = (bool)value;
        return isChecked ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

huangapple
  • 本文由 发表于 2023年4月11日 17:09:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75984192.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定