WPF - 删除“用户控件"StackPanel 中的子项 [英] WPF - Remove a "User Control" Child from a StackPanel

查看:49
本文介绍了WPF - 删除“用户控件"StackPanel 中的子项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个 WPF UI,用户可以在其中编辑查询以搜索数据库.查询是根据消费者从组合框Like This 中选择的内容创建的,他可以只要他点击添加新条件按钮,就可以创建任意数量的过滤器.

I'm trying to make a WPF UI where the user can edit a query to search the database. The query is created according to what the consumer chooses from the comboboxes Like This and he can create as much filters as he wants as long as he clicks the Add new Condition button.

我将组合框模板创建为这样的用户控件:

I created the comboboxes template as a User Control like this :

用户控制 XAML:

<StackPanel Orientation="Horizontal" >
        <Button
                Name="DeleteFilter" 
                HorizontalAlignment="Left"
                Margin="5"
                Content="-"
            Click="DeleteFilter_OnClick">
        </Button>
        <ComboBox 
                Text="Property"
                x:Name="Property"
                Width="100"
                DataContext="{StaticResource SomeViewModel}"
                ItemsSource="{Binding Properties}"
                DisplayMemberPath="Name"
             SelectionChanged="Property_OnSelectionChanged"/>
        <ComboBox 
            Text="PropertyOperator"
            x:Name="Operator"
            ItemsSource="{Binding Operators}"
            DisplayMemberPath="Name"
            SelectionChanged="Operator_OnSelectionChanged">
        </ComboBox>
        <TextBox 
                x:Name="Value"
                Text="Value"
                TextAlignment="Center"
                Width="100"
                Margin="5"/>
</StackPanel>

每当用户点击添加新条件按钮时,我都会调用这个事件:

Whenever the user clicks the Add new Condition button, I call this event:

private void AddFilterButton_OnClick(object sender, RoutedEventArgs e)
    {
        var conditionUserControl = new ConditionUserControl();
        StackPanel.Children.Add(conditionUserControl);
    }

一切正常.

我的问题:

如何通过单击 DeleteFilter 删除用户控件子项 存在于用户控件模板中的按钮.

How can I delete the User Control child from clicking the DeleteFilter button that exists in the User Control template.

我试过这个:

StackPanel.Children.Remove(..);

从我的 MainWindow 中删除孩子,但如何知道用户单击了哪个孩子.

to remove the child from my MainWindow but how to know which child the user clicked.

推荐答案

试试这个:

private void DeleteFilter_OnClick(object sender, RoutedEventArgs e)
{
    Button btn = sender as Button;
    var conditionUserControl = FindParent<ConditionUserControl>(btn);
    if (conditionUserControl != null)
    {
        var sp = FindParent<StackPanel>(conditionUserControl);
        if (sp != null)
            sp.Children.Remove(conditionUserControl);
    }
}


private static T FindParent<T>(DependencyObject dependencyObject) where T : DependencyObject
{
    var parent = VisualTreeHelper.GetParent(dependencyObject);

    if (parent == null) return null;

    var parentT = parent as T;
    return parentT ?? FindParent<T>(parent);
}

这篇关于WPF - 删除“用户控件"StackPanel 中的子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆