简体   繁体   English

防止在WPF C#中调整控件的大小

[英]Prevent resize of control in grid WPF C#

(Edit) i have an image in tabItem1. (编辑)我在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: 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);
  }

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. 如果您设置的属性VerticalAlignment (例如到Top )和HorizontalAlignment (例如到Left您的组件)的imagerect ,这些控件将根据内容需要的大小,而不是在容器中的可用空间。

Is that what you want ? 那是你要的吗 ?

EDIT : For your image, you should set its property Stretch="None" . 编辑:对于您的图像,您应该设置其属性Stretch="None" See here . 看这里

EDIT 2 : 编辑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
        };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM