我正在嘗試洗掉邊框內的文本塊。如果我單擊邊框它可以正常作業,但單擊文本塊什么也不做。
代碼: 創建:
Border newBorder = new Border
{
Height = 50,
Width = 50,
BorderBrush = new SolidColorBrush(Colors.Black),
BorderThickness = new Thickness(2),
CornerRadius = new CornerRadius(25),
};
TextBlock newTextBlock = new TextBlock
{
Background = customColor,
TextWrapping = TextWrapping.Wrap,
VerticalAlignment = VerticalAlignment.Center,
HorizontalAlignment = HorizontalAlignment.Center,
Text = numberOfNodes.ToString()
};
newBorder.Child = newTextBlock;
CanvasMain.Children.Add(newBorder);
消除:
if (e.Source is TextBlock)
{
TextBlock activeTextBlock = (TextBlock)e.OriginalSource;
if (removeTextBlock == true)
{
CanvasMain.Children.Remove(activeTextBlock);
}
}
當它只是一個矩形并且沒有問題時,我也這樣做了。
結果:需要一個帶有數字的圓圈,并且應該通過單擊將其洗掉。
uj5u.com熱心網友回復:
您將 添加Border到 的Children集合中Canvas并將 設定TextBlock為Child的Border。
因此,要洗掉TextBlock,您應該將其從而Border不是從 中洗掉Canvas:
private void CanvasMain_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.Source is TextBlock)
{
TextBlock activeTextBlock = (TextBlock)e.OriginalSource;
if (removeTextBlock == true && activeTextBlock.Parent is Border border)
{
border.Child = null;
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/527036.html
標籤:C#wpf
上一篇:可觀察驗證器在wpf中不起作用
