|
Тема |
WPF |
|
Автор |
wizardabvbg1 (непознат
) |
|
Публикувано | 02.09.08 17:47 |
|
|
Здравейте, имам един проблем. Имам един грид, в който динамично създавам един правоъгълник, след като се кликне на първия бутон. След това като кликна на втория бутон премествам правоъгълника в ListBox, и след това ако кликна на първия бутон да го върна обратно в грида дава грешка: "Specified Visual is already a child of another Visual or the root of a CompositionTarget.". Цялото това нещо го правя на WPF i C#. Ето кода по-долу, ако някой има идея как може да се фиксне проблема, нека пише. Благодаря предварително.
XAML:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid Name="myGrid">
<ListBox IsSynchronizedWithCurrentItem="True" VerticalContentAlignment="Top" Margin="0,63,0,27.004" ScrollViewer.CanContentScroll="False" ScrollViewer.VerticalScrollBarVisibility="Auto" BorderBrush="#FF000000" Padding="-4,-2,0,-2" x:Name="Teb2Rect2ListBox" Grid.IsSharedSizeScope="True" ScrollViewer.HorizontalScrollBarVisibility="Hidden">
<Rectangle Width="416" Height="114" Fill="#FFEDEDED" Stroke="#FF000000" RadiusX="0" RadiusY="0"/>
<Rectangle Width="416" Height="114" Fill="#FFE2E2E2" Stroke="#FF000000" RadiusX="0" RadiusY="0" StrokeThickness="1"/>
<Rectangle Width="416" Height="114" Fill="#FFE2E2E2" Stroke="#FF000000" RadiusX="0" RadiusY="0"/>
<Rectangle Width="416" Height="114" Fill="#FFEDEDED" Stroke="#FF000000" RadiusX="0" RadiusY="0"/>
</ListBox>
<Button Height="40" Margin="26,6,0,0" Name="button1" VerticalAlignment="Top" Click="button1_Click" HorizontalAlignment="Left" Width="95">Button</Button>
<Button Height="40" Margin="134,6,41,0" Name="button2" VerticalAlignment="Top" Click="button2_Click">Button</Button>
</Grid>
</Window>
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Rectangle R1 = new Rectangle();
public Window1()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
/* try
{ */
R1.Width = 100;
R1.Height = 100;
R1.SetValue(Canvas.LeftProperty, (double)1);
R1.SetValue(Canvas.TopProperty, (double)1);
R1.Fill = new SolidColorBrush(Color.FromArgb(59, 12, 154, 23));
R1.Stroke = new SolidColorBrush(Color.FromArgb(25, 34, 143, 154));
R1.StrokeThickness = 1;
R1.RenderTransformOrigin = new Point(0.5, 0.5);
R1.VerticalAlignment = VerticalAlignment.Top;
R1.HorizontalAlignment = HorizontalAlignment.Left;
R1.Visibility = Visibility.Visible;
Teb2Rect2ListBox.Items.Remove(R1);
Teb2Rect2ListBox.Items.Refresh();
//Teb2Rect2ListBox.Items.Clear();
myGrid.Children.Remove(R1);
//ContainerVisual cv = new ContainerVisual();
//cv.Children.Remove(R1);
//NonLogicalAdornerDecorator = null;
//VisualCollection.
try
{
UIElement parent = VisualTreeHelper.GetParent(R1) as UIElement;
MessageBox.Show(parent.ToString());
ContentPresenter cp = new ContentPresenter();
base.RemoveVisualChild(R1);
base.RemoveLogicalChild(R1);
}
catch { }
myGrid.Children.Add(R1);
/*}
catch (Exception eee)
{
MessageBox.Show("Exception " + eee.ToString());
}*/
}
private void button2_Click(object sender, RoutedEventArgs e)
{
myGrid.Children.Remove(R1);
Teb2Rect2ListBox.Items.Remove(R1);
Teb2Rect2ListBox.Items.Add(R1);
}
}
}
|
| |
|
|
|