在WPF中添加和删除控件 [英] Add and Remove controls in a WPF
问题描述
我有一个wpf表单,当用户检查某个无线电控件时,我要在其中添加用户控件.如果未选中控件,则需要删除用户控件.
这是我正在尝试做的事情:
I have a wpf form where I am adding a user control when the user checks a certain radio control. If the control is unchecked then I need for the user control to be removed.
Here is what I was trying to do:
<pre lang="vb">Private Sub CtlRadioStacker1_rdYesChecked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles CtlRadioStacker1.rdYesChecked<br />
Dim cbMaterial As New ctlComboBoxStacker<br />
If Me.CtlRadioStacker1.rdYes.IsChecked = True Then<br />
cbMaterial.Name = "cbMaterial"<br />
cbMaterial.Label.Content = "Material"<br />
StackPanelMain.Children.Add(cbMaterial)<br />
Else<br />
Dim location As Integer = StackPanelMain.FindName("cbMaterial")<br />
StackPanelMain.Children.RemoveAt(location)<br />
End If<br />
End Sub</pre><br />
这段代码可以很好地添加控件,但是它并没有像我期望的那样删除控件.毫不奇怪,如果我切换单选按钮,它会添加控件的其他副本而不会删除最后一个控件,我知道这一点,并且在解决此难题后会予以纠正.
任何帮助将不胜感激.
谢谢,
Jason
This code adds the control just fine, however it does not remove the control as I had expected. Not surprisingly if I toggle the radio buttons it adds additional copies of the control while never removing the last, I am aware of this and will correct it once I solve this dilemma.
Any help would be greatly appreciated.
Thanks,
Jason
推荐答案
您应该改为在Unchecked事件中将其删除.
以下是两种解决方案,具体取决于用于标识要删除的控件的标准:
You should remove it in the Unchecked event instead I think.
Here''s two solutions, depending on what criteria for identifying the control to remove:
Private Sub RadioButton_Checked(sender As Object, e As RoutedEventArgs)
Dim button As Button = New Button With {.Content = "Some content", .Name = "MyName"}
theStackpanel.Children.Add(button)
End Sub
Private Sub RadioButton_UnChecked(sender As Object, e As RoutedEventArgs)
' Remove by control type, repeat if there can be many of the same type
'theStackpanel.Children.Remove(theStackpanel.Children.Cast(Of UIElement).Where(Function(element) element.GetType() = GetType(Button)).First())
' Remove by control name
theStackpanel.Children.Remove(theStackpanel.Children.Cast(Of FrameworkElement).Where(Function(element) element.Name = "MyName").Single())
End Sub
希望这会有所帮助,
Fredrik Bornander
Hope this helps,
Fredrik Bornander
这是怎么做的.假设要添加的UserControl在Grid资源中定义.
Here is how did. Assuming the UserControl to be added is defined in the Resources of Grid.
<grid name="grid">
<grid.resources>
<local:ausercontrol x:key="myUserControl" xmlns:x="#unknown" xmlns:local="#unknown" />
</grid.resources>
<RadioButton Margin="28,29,0,0" Name="radioButton1" Height="16" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120">RadioButton</RadioButton>
<StackPanel Margin="28,81,26,28" Name="stackPanel1"/>
<RadioButton Height="16" HorizontalAlignment="Right" Margin="0,29,38,0" Name="radioButton2" VerticalAlignment="Top" Width="120">RadioButton</RadioButton>
</grid>
在我的代码后面,我这样做了:
And in my code-behind, I did this :
public class Window1
UserControl uc;
public Window1()
{
InitializeComponent();
radioButton1.Checked += new RoutedEventHandler(radioButton1_Checked);
radioButton1.Unchecked += new RoutedEventHandler(radioButton1_Unchecked);
}
void radioButton1_Unchecked(object sender, RoutedEventArgs e)
{
stackPanel1.Children.Remove(uc);
}
void radioButton1_Checked(object sender, RoutedEventArgs e)
{
uc = (UserControl)grid.FindResource("myUserControl");
stackPanel1.Children.Add(uc);
}
}
我添加了另一个radiobutton(radiobutton2),以便可以为radiobutton1处理未经检查的事件.
这对您有用吗?
I have added another radiobutton(radiobutton2) so that the unchecked event can be handled for radiobutton1.
Did this work for you?
这篇关于在WPF中添加和删除控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!