[WPF]auto和*总结

Auto和*效果

Auto 表示自动适应显示内容的宽度, 控件有多大,就显示多大。

* 则表示按比例来分配宽度。

 

话不多说,直接上例子理解

 

例子1

 

 

代码:

  <Grid ShowGridLines="True">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--控件-->
        <Button Grid.Row="0" Content="Auto按钮"/>
        <Button Grid.Row="1" Content="*按钮"/>
        <Button Grid.Row="2" Content="Auto按钮"/>
        <Button Grid.Row="3" Content="*按钮"/>
    </Grid>

可以看出,Auto表示自动适应控件的宽度,按钮默认多大就显示多大,而剩余的区域则由*去分配,由此可能导致出现一个问题,看下面这个例子。

 

例子2

 

代码:

//XAML部分代码:
<Grid ShowGridLines="True">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <!--控件-->
        <Button Grid.Row="0" Content="Auto按钮"/>
        <Button Grid.Row="1" Content="Auto按钮"/>
        <Image Grid.Row="2" Name="Imagebox"></Image>
        <Button Grid.Row="3" Content="Auto按钮"/>
</Grid>




//后台部分
public MainWindow()
{
      InitializeComponent();
      var image1 = new System.Windows.Media.Imaging.BitmapImage();
      image1.BeginInit();
      image1.UriSource = new Uri(@"F:\测试图片\test\IMG10201.jpg", UriKind.Absolute);
      image1.EndInit();
      Imagebox.Source = image1;
}

可以看出,全部用Auto的话,从上往下渲染,Image的尺寸过大,程序已经显示不下了,把后面的按钮也抵消掉了,这里我们就需要换一种思路,改成下面这种:

代码:

//XAML代码
 <Grid ShowGridLines="True">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <!--控件-->
        <Button Grid.Row="0" Content="Auto按钮"/>
        <Button Grid.Row="1" Content="Auto按钮"/>
        <Image Grid.Row="2" Name="Imagebox"></Image>
        <Button Grid.Row="3" Content="Auto按钮"/>
</Grid>

//后台代码和上面一样不变,这里就不写了

GIF效果图:

我们会发现在三个使用了Auto的Grid中,会先自动按照控件的大小进行分配高度,分配完成之后剩下的部分就由使用了*的部分去分配高度。

 

当然,具体问题具体分析,项目有不同的需求的话也需要尝试各种布局规则。

总而言之,先分配Auto,最后分配*,Auto有多大就显示多大,也不会管存在显示不下的问题,需要自行解决

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在C#中使用WPF窗口来开发AutoCAD插件,你需要使用AutoCAD .NET API和WPF技术。以下是基本步骤: 1. 在Visual Studio中创建一个新的Class Library项目。 2. 添加对AutoCAD .NET API的引用。这可以通过添加对acdbmgd.dll和acmgd.dll的引用来完成。 3. 在项目中添加一个新的WPF窗口或用户控件。 4. 在WPF窗口中添加必要的控件和事件处理程序。 5. 在AutoCAD中加载插件并在需要时显示WPF窗口。 以下是一个简单的示例,演示如何在AutoCAD中使用WPF窗口: ```csharp using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using System.Windows.Controls; using System.Windows.Forms.Integration; namespace MyPlugin { public class MyCommands { [CommandMethod("MyCommand")] public void MyCommand() { // 获取当前文档和编辑器 Document doc = Application.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; // 创建并显示WPF窗口 MyWpfWindow wpfWindow = new MyWpfWindow(); ElementHost.EnableModelessKeyboardInterop(wpfWindow); Autodesk.AutoCAD.ApplicationServices.Application.ShowModalWindow(wpfWindow); // 在控制台中显示选定的文本 PromptSelectionResult selRes = ed.GetSelection(); if (selRes.Status == PromptStatus.OK) { SelectionSet selSet = selRes.Value; foreach (SelectedObject selObj in selSet) { if (selObj.ObjectId.ObjectClass == RXClass.GetClass(typeof(DBText))) { DBText text = (DBText)selObj.ObjectId.GetObject(OpenMode.ForRead); ed.WriteMessage("Selected Text: " + text.TextString); } } } } } public class MyWpfWindow : UserControl { public MyWpfWindow() { // 添加WPF控件 TextBox textBox = new TextBox(); textBox.Text = "Hello, world!"; this.Content = textBox; } } } ``` 在这个示例中,我们在AutoCAD中创建了一个名为"MyCommand"的命令,当用户输入该命令时,会打开一个WPF窗口,并在控制台中显示选定的文本。WPF窗口中只包含一个文本框控件,其中包含"Hello, world!"文本。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值