WPF RowDefinition “collapse” 当 ActualHeight 低于阈值时将高度设置为 0

huangapple go评论103阅读模式
英文:

WPF RowDefinition "collapse" as set height to 0 when Actualheight below threshold

问题

在我的应用程序中,我使用代码生成GUI元素,并将它们绑定到对象列表的属性上。
这些GUI元素中包含了一个包括TextEdits(devexpress)、小图标和SpinEdits(devexpress)等各种其他元素的网格。

它们受窗口大小的影响。如果大小变得太小,控件会被挤压,文本将无法阅读,看起来很糟糕。

第一行应该始终可读。我用以下代码实现了这一点:

``` lang-vbnet
newRow1.MinHeight = 30

现在我想根据它们的实际大小来“折叠”其他行。例如,如果它们的高度小于30像素,我想将它们折叠起来,因为它们的内容无论如何都无法阅读。

如果窗口放大,它们应该再次变得可见。

这看起来很有前途,但是因为行没有Visibility属性,我不知道如何调整这个解决方案。
https://stackoverflow.com/questions/42009617/can-i-trigger-an-event-in-xaml-when-a-control-is-less-than-a-certain-height

这是我在代码中创建网格的方式:

Dim NewGridSchalter As New System.Windows.Controls.Grid
Dim newRow1 As New RowDefinition
Dim newRow2 As New RowDefinition
Dim newRow3 As New RowDefinition

newRow1.MinHeight = 30

NewGridSchalter.RowDefinitions.Add(newRow1)
NewGridSchalter.RowDefinitions.Add(newRow2)
NewGridSchalter.RowDefinitions.Add(newRow3)

Dim NewColumnBild As New ColumnDefinition
Dim NewColumnText As New ColumnDefinition
NewGridSchalter.ColumnDefinitions.Add(NewColumnBild)
NewGridSchalter.ColumnDefinitions.Add(NewColumnText)

NewGridSchalter.ShowGridLines = False
NewGridSchalter.HorizontalAlignment = HorizontalAlignment.Stretch
NewGridSchalter.VerticalAlignment = VerticalAlignment.Stretch

之后,网格会被修改并填充其他控件。
如果可能的话,我不想单独折叠控件。

它必须是一个网格。我尝试过使用堆栈面板或停靠面板,但我需要在其中放置控件并控制它们占用多少空间的能力。


<details>
<summary>英文:</summary>

In my application I generate GUI-elements in code and bind them to properties in a list of objects.  
Each of these GUI-elements contains a Grid with various other elements like TextEdits (devexpress), small icons and SpinEdits(devexpress).

They are subjective to the window size. If the size gets to small the controls will get squeezed and text is no longer readable and it looks awful.

The first row should always be readable. I achieved this with

``` lang-vbnet
newRow1.MinHeight = 30

Now I want to "collapse" the other rows depending on their actual size. Per example if they have <30 px height I want to collapse them because their content isn't readable anyway.

If the window is enlarged they should become visible again.

This looks promising but because rows don't have a Visibility property I don't know how to adapt this solution.
https://stackoverflow.com/questions/42009617/can-i-trigger-an-event-in-xaml-when-a-control-is-less-than-a-certain-height

This is how I create the Grid in code

Dim NewGridSchalter As New System.Windows.Controls.Grid
Dim newRow1 As New RowDefinition
Dim newRow2 As New RowDefinition
Dim newRow3 As New RowDefinition

newRow1.MinHeight = 30

NewGridSchalter.RowDefinitions.Add(newRow1)
NewGridSchalter.RowDefinitions.Add(newRow2)
NewGridSchalter.RowDefinitions.Add(newRow3)

Dim NewColumnBild As New ColumnDefinition
Dim NewColumnText As New ColumnDefinition
NewGridSchalter.ColumnDefinitions.Add(NewColumnBild)
NewGridSchalter.ColumnDefinitions.Add(NewColumnText)

NewGridSchalter.ShowGridLines = False
NewGridSchalter.HorizontalAlignment = HorizontalAlignment.Stretch
NewGridSchalter.VerticalAlignment = VerticalAlignment.Stretch

After that the grid is changed and filled with other controls.
If possible I don't want to collapse the controls individually.

It has to be a grid. I tried this with stackpanels or dockpanels but I need the ability to place controls in there where I want them to and how much space they get.

答案1

得分: 0

这是你要翻译的部分:

找到了一个解决方案。

可以将以下代码添加到我提出的问题的代码末尾。重要的是要使用网格的SizeChanged事件,而不是行的SizeChanged事件,否则它会开始跳来跳去。

AddHandler NewGridSchalter.SizeChanged, Function()
   If NewGridSchalter.ActualHeight < 70 Then
      newRow2.Height = New GridLength(0, GridUnitType.Pixel)
      newRow3.Height = New GridLength(0, GridUnitType.Pixel)
   Else
      newRow2.Height = New GridLength(1, GridUnitType.Star)
      newRow3.Height = New GridLength(1, GridUnitType.Star)
   End If
End Function
英文:

Found a solution.

This piece of code can be added to the end of the code in my question. Its important to use the SizeChanged event of the grid and not of a row otherwise it will start to jump around.

AddHandler NewGridSchalter.SizeChanged, Function()
   If NewGridSchalter.ActualHeight &lt; 70 Then
	  newRow2.Height = New GridLength(0, GridUnitType.Pixel)
	  newRow3.Height = New GridLength(0, GridUnitType.Pixel)
   Else
	  newRow2.Height = New GridLength(1, GridUnitType.Star)
	  newRow3.Height = New GridLength(1, GridUnitType.Star)
   End If
End Function

huangapple
  • 本文由 发表于 2023年6月21日 23:09:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76524770.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定