-1
用戶控制我的GroupBox其中包含多個用戶控件,有刪除按鈕,每個用戶的控制,請指導我如何刪除用戶控制當我刪除按鈕點擊如何刪除WPF
用戶控制我的GroupBox其中包含多個用戶控件,有刪除按鈕,每個用戶的控制,請指導我如何刪除用戶控制當我刪除按鈕點擊如何刪除WPF
<UserControl x:Class="WpfApplication1.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" Height="60" Width="150">
<Grid>
<Border BorderThickness="2" BorderBrush="Red" Background="Black">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Text="Abcd" Background="Red"/>
<Button Content="Delete" Grid.Row="1" Click="Button_Click"/>
</Grid>
</Border>
</Grid>
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public Action Delete;
private void Button_Click(object sender, RoutedEventArgs e)
{
if(Delete!=null)
Delete();
}
}
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:uc="clr-namespace:WpfApplication1"
xmlns:local="clr-namespace:WpfApplication1"
Width="1000" Height="1000"
Title="MainWindow" x:Name="abc">
<Grid>
<GroupBox Header="Group">
<GroupBox.Content>
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<local:UserControl1 x:Name="uc1"/>
<local:UserControl1 x:Name="uc2" Grid.Row="1"/>
</Grid>
</GroupBox.Content>
</GroupBox>
</Grid>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
uc2.Delete += OnDelete;
}
private void OnDelete()
{
LayoutRoot.Children.Remove(uc2);
}
}
我希望這會幫助你。
嗨它的工作正常,但只有一個用戶控制,你可以指導我如何調用兩個以上的用戶控制 – Ghufran 2013-02-14 04:10:05
感謝它運作良好,我只是通過用戶控制的參考 – Ghufran 2013-02-14 06:18:12
這雖然它回答了這個問題,與良好的WPF設計是對立的。 – Will 2013-02-14 15:51:36