C# 如何在不移动文本的情况下更改文本块的边距?

C# 如何在不移动文本的情况下更改文本块的边距?,c#,wpf,windows,C#,Wpf,Windows,每当我尝试调整文本块的大小时,通常文本都会随之移动。有没有办法避免这种行为,这样我就可以在不移动文本的情况下调整背景的大小 我已经摆弄了太多的设置,四处询问了几次聊天,但都没有回答 或者,是否有方法将文本放置在文本块的绝对中心?向下调整文本块的大小时,文本不会移动,但在任何其他方向,文本都会移动。我曾经处理过文本的水平和垂直对齐等问题,但也没有做任何修改。这是一个测试代码,它可以实现您想要的功能,或者至少可以为您提供一些想法。 在屏幕的底部,您可以更改上部文本块的边距、宽度和高度 代码正在计算文

每当我尝试调整文本块的大小时,通常文本都会随之移动。有没有办法避免这种行为,这样我就可以在不移动文本的情况下调整背景的大小

我已经摆弄了太多的设置,四处询问了几次聊天,但都没有回答


或者,是否有方法将文本放置在文本块的绝对中心?向下调整文本块的大小时,文本不会移动,但在任何其他方向,文本都会移动。我曾经处理过文本的水平和垂直对齐等问题,但也没有做任何修改。

这是一个测试代码,它可以实现您想要的功能,或者至少可以为您提供一些想法。 在屏幕的底部,您可以更改上部文本块的边距、宽度和高度

代码正在计算文本块左/顶部位置的移动。然后设置文本块的填充以补偿该移动。如果文本超出文本块的左侧或顶部,则填充设置为0,并将跟随文本块。如果文本超出文本块的右/底部,您必须注意该怎么做

计算在LayoutUpdated事件处理程序中完成,该处理程序在面板中完成所有布局安排后调用

XAML:


代码隐藏:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        DataContext = this;
        InitializeComponent();
        Loaded += MainWindow_Loaded;
    }

    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        lastTextBlockPos = textBlock.PointToScreen(new Point());
        initiateReady = true;
    }

    bool initiateReady = false;
    Point lastTextBlockPos = new Point();
    private void Grid_LayoutUpdated(object sender, EventArgs e)
    {
        if (initiateReady)
        {
            Point tp = textBlock.PointToScreen(new Point());
            if (tp != lastTextBlockPos)
            {
                //Calculate how much the top left corner of the text block moved during layout.
                double left = textBlock.Padding.Left - tp.X + lastTextBlockPos.X;
                double top = textBlock.Padding.Top - tp.Y + lastTextBlockPos.Y;

                //Set the padding to move the text inside the text block
                //If outside the text block, set to left, top most position
                //TODO: take care of when the text is outside on the right, bottom side
                Thickness tn = new Thickness()
                {
                    Left = left >= 0 ? left : 0,
                    Top = top >= 0 ? top : 0,
                };
                textBlock.Padding = tn;
                lastTextBlockPos = tp;

                //Showing TextBlock bounds for test
                Point gp = grid.PointToScreen(new Point());
                RectangleGeometry myRectangleGeometry = new RectangleGeometry();
                myRectangleGeometry.Rect = new Rect(tp.X - gp.X, tp.Y - gp.Y, textBlock.ActualWidth, textBlock.ActualHeight);
                Path myPath = new Path();
                myPath.Data = myRectangleGeometry;
                myPath.Stroke = Brushes.Goldenrod;
                myPath.StrokeThickness = 2;
                Grid.SetColumn(myPath, 0);
                Grid.SetRow(myPath, 0);
                Path old = grid.Children.OfType<Path>().FirstOrDefault();
                if (old != null) grid.Children.Remove(old);
                grid.Children.Add(myPath);
            }
        }
    }
}
公共部分类主窗口:窗口
{
公共主窗口()
{
DataContext=this;
初始化组件();
加载+=主窗口\u加载;
}
已加载私有void主窗口(对象发送器、路由目标)
{
lastTextBlockPos=textBlock.PointToScreen(新点());
initiateReady=true;
}
bool initiaterady=false;
Point lastTextBlockPos=新点();
私有无效网格\u布局已更新(对象发送方,事件参数e)
{
如果(发起人日)
{
Point tp=textBlock.PointToScreen(新点());
如果(tp!=lastTextBlockPos)
{
//计算布局期间文本块左上角的移动量。
左双=textBlock.Padding.left-tp.X+lastTextBlockPos.X;
double-top=textBlock.Padding.top-tp.Y+lastTextBlockPos.Y;
//设置填充以在文本块内移动文本
//如果在文本块之外,则设置为最左上方位置
//待办事项:注意文本位于右侧、底部外侧时
厚度tn=新厚度()
{
左=左>=0?左:0,
顶部=顶部>=0?顶部:0,
};
textBlock.Padding=tn;
lastTextBlockPos=tp;
//显示测试的文本块边界
Point gp=grid.PointToScreen(新点());
矩形几何体myRectangleGeometry=新矩形几何体();
myRectangleGeometry.Rect=新的Rect(tp.X-gp.X,tp.Y-gp.Y,textBlock.ActualWidth,textBlock.ActualHeight);
路径myPath=新路径();
myPath.Data=myRectangleGeometry;
myPath.Stroke=画笔.一枝黄花;
myPath.StrokeThickness=2;
Grid.SetColumn(myPath,0);
Grid.SetRow(myPath,0);
Path old=grid.Children.OfType().FirstOrDefault();
if(old!=null)grid.Children.Remove(old);
grid.Children.Add(myPath);
}
}
}
}

您能提供xaml吗?我不明白为什么要增加边距来增加宽度/高度。
public partial class MainWindow : Window
{
    public MainWindow()
    {
        DataContext = this;
        InitializeComponent();
        Loaded += MainWindow_Loaded;
    }

    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        lastTextBlockPos = textBlock.PointToScreen(new Point());
        initiateReady = true;
    }

    bool initiateReady = false;
    Point lastTextBlockPos = new Point();
    private void Grid_LayoutUpdated(object sender, EventArgs e)
    {
        if (initiateReady)
        {
            Point tp = textBlock.PointToScreen(new Point());
            if (tp != lastTextBlockPos)
            {
                //Calculate how much the top left corner of the text block moved during layout.
                double left = textBlock.Padding.Left - tp.X + lastTextBlockPos.X;
                double top = textBlock.Padding.Top - tp.Y + lastTextBlockPos.Y;

                //Set the padding to move the text inside the text block
                //If outside the text block, set to left, top most position
                //TODO: take care of when the text is outside on the right, bottom side
                Thickness tn = new Thickness()
                {
                    Left = left >= 0 ? left : 0,
                    Top = top >= 0 ? top : 0,
                };
                textBlock.Padding = tn;
                lastTextBlockPos = tp;

                //Showing TextBlock bounds for test
                Point gp = grid.PointToScreen(new Point());
                RectangleGeometry myRectangleGeometry = new RectangleGeometry();
                myRectangleGeometry.Rect = new Rect(tp.X - gp.X, tp.Y - gp.Y, textBlock.ActualWidth, textBlock.ActualHeight);
                Path myPath = new Path();
                myPath.Data = myRectangleGeometry;
                myPath.Stroke = Brushes.Goldenrod;
                myPath.StrokeThickness = 2;
                Grid.SetColumn(myPath, 0);
                Grid.SetRow(myPath, 0);
                Path old = grid.Children.OfType<Path>().FirstOrDefault();
                if (old != null) grid.Children.Remove(old);
                grid.Children.Add(myPath);
            }
        }
    }
}