简体   繁体   English

WPF-从StackPanel中删除“用户控件”子级

[英]WPF - Remove a “User Control” Child from a StackPanel

I'm trying to make a WPF UI where the user can edit a query to search the database. 我正在尝试制作一个WPF UI,用户可以在其中编辑查询以搜索数据库。 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 : 我创建了comboboxes模板作为用户控件,如下所示:

User control XAML: 用户控件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);
    }

Everything works correctly. 一切正常。

My Question: 我的问题:

How can I delete the User Control child from clicking the DeleteFilter button that exists in the User Control template . 如何通过单击用户控件模板中存在DeleteFilter 按钮删除用户控件子级。

I tried this: 我尝试了这个:

StackPanel.Children.Remove(..);

to remove the child from my MainWindow but how to know which child the user clicked. 从我的MainWindow中删除该子项,但是如何知道用户单击了哪个子项。

Try this: 尝试这个:

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);
}

Another answer to @mm8 answer is : @ mm8答案的另一个答案是:

Update the AddFilterButton_OnClick: 更新AddFilterButton_OnClick:

I did this and the functionality works: 我做到了,功能正常运行:

private void AddAndFilterButton_OnClick(object sender, RoutedEventArgs e)
    {
        var conditionUserControl = new ConditionUserControl();

        StackPanel.Children.Add(conditionUserControl);

        conditionUserControl.DeleteFilter.Click += (o, args) => StackPanel.Children.Remove(conditionUserControl);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM