0

(Edit) i have an image in tabItem1. when i resize window(dragging by corner or maximize button) the image also resize and occupy whole grid. i added width and height on image and the resisng stopped default to actual image width & height in pixels.

Do i have to apply width and height to prevent resising of control whom i don't want to resize on window scale? or is there any property for controls to prevent resizing.

Basically, i'll have some pics which i don't want to be resided, and there will be some text which i want to be resided.

XAML:

<Window x:Class="Engine.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Width="600" Height="600">
    <Grid>
        <TabControl Grid.RowSpan="2">
            <TabItem Header="TabItem1">
                <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
                    <Grid x:Name="TGrid1" Background="#FFE5E5E5"/>
                </ScrollViewer>
            </TabItem>
            <TabItem Header="TabItem2">
                <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
                    <Grid x:Name="TGrid2" Background="#FFE5E5E5">
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                    </Grid>
                </ScrollViewer>
            </TabItem>
        </TabControl>
    </Grid>
</Window>

Code:

public MainWindow()
{
        InitializeComponent();

        var bitmapFrame = BitmapFrame.Create(new Uri(@"" + AppDomain.CurrentDomain.BaseDirectory + "Chrysanthemum.jpg"), BitmapCreateOptions.DelayCreation, BitmapCacheOption.None);
        var dragDropImage = new Image
        {
            Source = bitmapFrame, //new BitmapImage(new Uri(@"" + AppDomain.CurrentDomain.BaseDirectory + "Chrysanthemum.jpg")),
            Name = "dragDropImage",
            Width = bitmapFrame.PixelWidth,
            Height = bitmapFrame.PixelHeight
        };
        TGrid1.Children.Add(dragDropImage);

        var rect = new Rectangle
        {
            Stroke = new SolidColorBrush(Colors.Red),
            Fill = new SolidColorBrush(Colors.Black),
            Width = 474,
            Height = 405
        };
        Grid.SetRow(rect, 0);
        TGrid2.Children.Add(rect);
  }
ADi
  • 219
  • 1
  • 5
  • 17

1 Answers1

4

If you set the properties VerticalAlignment (for example to Top) and HorizontalAlignment (for example to Left) of your components image and rect, these controls will be sized according to the content need, instead of the available space in the container.

Is that what you want ?

EDIT : For your image, you should set its property Stretch="None". See here.

EDIT 2 :

var dragDropImage = new Image
        {
            Source = bitmapFrame, //new BitmapImage(new Uri(@"" + AppDomain.CurrentDomain.BaseDirectory + "Chrysanthemum.jpg")),
            Name = "dragDropImage",
            VerticalAlignment = System.Windows.VerticalAlignment.Top,
            HorizontalAlignment = System.Windows.HorizontalAlignment.Right,
            Stretch = System.Windows.Media.Stretch.None
        };
Ben
  • 54,723
  • 49
  • 178
  • 224
JYL
  • 8,228
  • 5
  • 39
  • 63
  • Dis you put a comma after `Height = bitmapFrame.PixelHeight` ? – JYL Dec 29 '13 at 20:43
  • yes. i added coma at second last item. i added this in constructor initializer but it gives error HorizontalAlignment = Left, error: cannot convert System.Windows.HorizonalALignment – ADi Dec 29 '13 at 20:45
  • applied, and image is not expanding but the rect is not showing if added vertical and horizontal, – ADi Dec 29 '13 at 20:51
  • 1
    yes, because the rect has no content and so can not define what is its ideal size (which is set to 0;0). The `Width` and `Height` are mandatory for a shape if you define the `VerticalAlignment` and `HorizontalAlignment` to something other than `Stretch`. – JYL Dec 29 '13 at 20:54
  • (Edit) 2nd line is very informative thanks, Got it working, needed to specify width and height according to ur 2nd line – ADi Dec 29 '13 at 21:00
  • You can't supply content to a rect (as you said it's just a colored box). You must define its Height and Width, except if you let the default value to VerticalAlignment and HorizontalAlignment, which is Stretch. But in this case, it will extend during the resize. – JYL Dec 29 '13 at 21:03
  • IF u may not mind, can u help with one last part i have to do. basically i am making a drag drop rect. i want to drag rect1 and drop in other rect2. question is: how can i put some text in middle of my rect. like a button, or may i open a new question plz – ADi Dec 29 '13 at 21:06
  • Please open a new question. – JYL Dec 29 '13 at 21:08