2017-08-15 32 views
0

我正在試圖製作一個WPF UI,用戶可以在其中編輯查詢來搜索數據庫。該查詢根據消費者從組合框Like This中選擇的內容創建,並且他可以創建儘可能多的過濾器,只要他點擊按鈕即可。WPF - 從StackPanel中刪除「用戶控件」子代

我創建的組合框模板的用戶控制像這樣:

用戶控制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> 

每當用戶點擊新增條件按鈕,我調用此事件:

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

一切工作正常。

我的問題:

如何我可以從點擊DeleteFilterbutton that exists in the User Control template刪除了用戶控制的孩子。

我嘗試這樣做:

StackPanel.Children.Remove(..); 

從我的主窗口取出孩子,但如何知道用戶點擊其中的孩子。

+0

如何該按鈕知道要刪除哪個孩子? 'StackPanel'不會爲您提供'SelectedItem' ..除非您爲每個子項添加按鈕或以某種方式自己處理選擇內容..您是否只想[刪除最後一個](https://stackoverflow.com/q/28208686/1997232)? – Sinatr

+0

不,我想刪除用戶點擊的任何孩子,這可能嗎?這纔是重點。我是新來的WPF,我不知道如何使這可能...你認爲它是一個很好的想法,添加到每個創建的孩子的按鈕? –

+0

*「將按鈕添加到每個創建的孩子」* - 它是完全沒問題的。我沒有注意到你已經這麼做了。您可以在Visual樹中找到包含按鈕的用戶控件,並從'StackPanel'中刪除該實例。 – Sinatr

回答

1

試試這個:

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

我無法理解答案。 AddFilterButton應該將子項添加到堆棧面板。實現你建議刪除這個功能..我是新來的WPF,如果我聽起來不方便的道歉。 –

+0

對不起,這是DeleteFilter_OnClick事件處理程序的實現。我改了名字。 – mm8

+0

我明白了。 AddFilter是FilterWindow類中的一個按鈕,DeleteFilter按鈕位於另一個名爲ConditionUserControl的類中。因此,StackPanel無法識別。 –

0

另一個答案@ MM8答案是:

更新AddFilterButton_OnClick:

我這樣做和功能的工作原理:

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