複数のテキストボックス、コンボボックス、およびボタンを持つ2つのWPFユーザーコントロールを作成しました。メインのwpfウィンドウでは、キャンバスとその中に2つのリスト項目を持つリストボックスを作成しました。私の要件は、最初のリストボックス項目を選択した場合、最初のユーザーコントロールがキャンバスに追加されるはずです。 2番目のリストボックス項目を選択すると、以前に追加されたユーザーコントロールが非表示になり、2番目のリストボックス項目が追加/表示されます。誰でもこのコードのサンプルを提供できますか?WPFのキャンバスにユーザーコントロールを追加/削除する
以下は私が書いたコードです。私は2つのユーザーコントロールを作成しました。
UserControl1.xaml
<UserControl x:Class="UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="210" Height="210" x:Name="UCntl1">
<Grid>
<StackPanel>
<GroupBox Header="Text Boxes">
<StackPanel Orientation="Horizontal">
<StackPanel>
<Label Margin="4" Height="21">TextBox1</Label>
<Label Margin="4" Height="21">TextBox2</Label>
<Label Margin="4" Height="21">TextBox3</Label>
</StackPanel>
<StackPanel>
<TextBox Width="100" Height="21" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="4">0.1</TextBox>
<TextBox Width="100" Height="21" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="4">0.2</TextBox>
<TextBox Width="100" Height="21" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="4">0.3</TextBox>
</StackPanel>
</StackPanel>
</GroupBox>
<GroupBox Header="Conbo Boxes">
<StackPanel Orientation="Horizontal">
<StackPanel>
<Label Margin="4" Height="21">ComboBox1</Label>
<Label Margin="4" Height="21">ComboBox2</Label>
<Label Margin="4" Height="21">ComboBox3</Label>
</StackPanel>
<StackPanel>
<ComboBox Width="100" Height="21" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="4">
<ComboBoxItem>Item AAA</ComboBoxItem>
</ComboBox>
<ComboBox Width="100" Height="21" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="4">
<ComboBoxItem>Item BBB</ComboBoxItem>
</ComboBox>
<ComboBox Width="100" Height="21" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="4">
<ComboBoxItem>Item CCC</ComboBoxItem>
</ComboBox>
</StackPanel>
</StackPanel>
</GroupBox>
</StackPanel>
</Grid>
</UserControl>
UserControl2.xaml
<UserControl x:Class="UserControl2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="210" Height="210" x:Name="UCntl2">
<Grid>
<StackPanel>
<GroupBox Header="Conbo Boxes">
<StackPanel Orientation="Horizontal">
<StackPanel>
<Label Margin="4" Height="21">ComboBox1</Label>
<Label Margin="4" Height="21">ComboBox2</Label>
<Label Margin="4" Height="21">ComboBox3</Label>
</StackPanel>
<StackPanel>
<ComboBox Width="100" Height="21" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="4"></ComboBox>
<ComboBox Width="100" Height="21" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="4"></ComboBox>
<ComboBox Width="100" Height="21" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="4"></ComboBox>
</StackPanel>
</StackPanel>
</GroupBox>
<GroupBox Header="Text Boxes">
<StackPanel Orientation="Horizontal">
<StackPanel>
<Label Margin="4" Height="21">TextBox1</Label>
<Label Margin="4" Height="21">TextBox2</Label>
<Label Margin="4" Height="21">TextBox3</Label>
</StackPanel>
<StackPanel>
<TextBox Width="100" Height="21" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="4"></TextBox>
<TextBox Width="100" Height="21" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="4"></TextBox>
<TextBox Width="100" Height="21" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="4"></TextBox>
</StackPanel>
</StackPanel>
</GroupBox>
</StackPanel>
</Grid>
Window1.xaml
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="Auto" Width="Auto">
<Grid>
<StackPanel Orientation="Horizontal">
<Grid Margin="0,0,0,0" Width="Auto" Height="Auto" VerticalAlignment="Top">
<ListBox Margin="0,0,0,0" HorizontalAlignment="Left" Width="Auto" Height="Auto" VerticalAlignment="Top" BorderBrush="White">
<ListBoxItem Name="LstItem1" Selected="LstItem1_Selected">User Control 1</ListBoxItem>
<ListBoxItem Name="LstItem2" Selected="LstItem2_Selected">User Control 2</ListBoxItem>
</ListBox>
</Grid>
<Grid Width="10" Background="LightGray"></Grid>
<Grid Margin="0,0,0,0">
<Canvas Name="Canvas1" Width="210" Height="210" VerticalAlignment="Top">
</Canvas>
</Grid>
</StackPanel>
</Grid>
Widow1.xaml.vb
Class Window1
Private Sub LstItem1_Selected(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
Canvas1.Children.Clear()
Canvas1.Children.Add(New UserControl1)
End Sub
Private Sub LstItem2_Selected(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
Canvas1.Children.Clear()
Canvas1.Children.Add(New UserControl2)
End Sub
エンドクラス
問題
私はキャンバスの前のコントロールを削除した後、ユーザーコントロールを追加しました。このため、UserControlsのリストボックス内の選択された値は、コントロールをキャンバスに再度追加している間にクリアされています。キャンバスからコントロールを削除する代わりに、キャンバス上でユーザーコントロールを非表示にする方法はありますか?
あなたは私の質問に上記の投稿のコードを見て、あなたがこの覗くため、この – Rahul
Boogaartの感謝のためのすべてのソリューションを持っているなら、私に返信してくださいでした。私は答えを得た。 Canvas(CanvasForm.Clear)からコントロールを削除する代わりに、コントロールの表示をCollapsedに設定する必要があります。私はこの回答を投稿しました。私がこの分野(WinFormsとWPF)に慣れていないので、私はvisibiltyプロパティを知らない。もう一度あなたの時間に感謝します。 – Rahul