Why is ClipToBounds = false not working?

I don't want to cut the text of the text block. For this reason I set viewBox.ClipToBounds to false, but it doesn't work.

Please tell me why ClipToBounds = false doesn't work in this code:

   private void Btn1_Click(object sender, RoutedEventArgs e)
    {
            Button button = new Button(); button.Background = Brushes.Red;
            button.Width = 70; button.Height = 20;
            Canvas.SetLeft(button, 100); Canvas.SetTop(button, 120);
            button.Padding = new Thickness(1);

            StackPanel stackPanel = new StackPanel();

            Viewbox viewBox = new Viewbox();
             viewBox.ClipToBounds = false;

            Canvas canvas = new Canvas();
             canvas.Width = button.Width; canvas.Height = button.Height;

            TextBlock textBlock = new TextBlock();
            textBlock.Text = "this is a test";
            textBlock.FontSize = 15;
            textBlock.FontFamily = new FontFamily("Arial");
            textBlock.TextWrapping = TextWrapping.NoWrap;
            textBlock.Foreground = Brushes.Green;
            textBlock.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
            textBlock.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
            viewBox.Height = 20;
            textBlock.IsHitTestVisible = false;

            stackPanel.Children.Add(viewBox);
            viewBox.Child = canvas;
            canvas.Children.Add(textBlock);
            button.Content = stackPanel;

            Canvas MainCanvas = new Canvas();
            MainCanvas.Children.Add(button);
            this.Content = MainCanvas;
    }

      

Screenhsot:

The screenshot below is what I want .:

+3


source to share


2 answers


ClipToBounds

by default false

. However, clipping can still occur due to the way some elements perform layout. Basically the way it works in WPF, the customization ClipToBounds = true

will make things jam. Leaving it equal false

, it means that WPF determines how things should be copied based on size constraints and positioning rectangles.

If you look at the methods in ArrangeCore

and MeasureCore

in FrameworkElement

, you will see that there is quite a lot of logic in determining whether to clamp something. Of course things that override FrameworkElement

can render, but they want to, but usually they will obey the clipping rules set by the base class.

In case, TextBlock

it will certainly clamp text outside of its bounds if its size is limited. You can see this by simply setting on it, Width

or placing it in the parent that has Width

.



If you really need text to render outside the bounds of the control, you might have to consider something like writing a custom text renderer. Even then, it will still be trimmed by its parent once you place it in something else that clips. This way, you can still get stuck.

You can try placing TextBlock

on top of the button, not inside it, and setting your position to get it in the right place (perhaps by binding it to something). This will work, but it can be difficult for you to handle if you have to do it too much.

Basically, you're trying to go against one of WPF's hard-coded rules, so you're unlikely to find an easy way to do it. You might want to reevaluate your design and determine if this behavior is really necessary for what you want to do, or if you can go about it differently.

+4


source


Thanks to elgonzo and Xavier.

I figured out that I shouldn't put the canvas in the viewport.

After 2 change my problem.

1 - Change viewport with canvas.



2 - Delete canvas.with = ...

This is the correct code:

 private void Btn1_Click(object sender, RoutedEventArgs e)
    {
        Button button = new Button(); button.Background = Brushes.Red;
        button.Width = 70; button.Height = 20;
        Canvas.SetLeft(button, 100); Canvas.SetTop(button, 120);
        button.Padding = new Thickness(1);

        StackPanel stackPanel = new StackPanel();

        Viewbox viewBox = new Viewbox();
        viewBox.ClipToBounds = false;

        Canvas canvas = new Canvas();
       // canvas.Width = button.Width; canvas.Height = button.Height;

        TextBlock textBlock = new TextBlock();
        textBlock.Text = "this is a test";
        textBlock.FontSize = 15;
        textBlock.FontFamily = new FontFamily("Arial");
        textBlock.TextWrapping = TextWrapping.NoWrap;
        textBlock.Foreground = Brushes.Green;
        textBlock.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
        textBlock.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
        viewBox.Height = 20;
        textBlock.IsHitTestVisible = false;

        stackPanel.Children.Add(canvas);
        viewBox.Child = textBlock;
        canvas.Children.Add(viewBox);
        button.Content = stackPanel;

        Canvas MainCanvas = new Canvas();
        MainCanvas.Children.Add(button);
        this.Content = MainCanvas;

    }

      

+2


source







All Articles