我正在 XAML 中開發 WPF 應用程式。我是初學者,我的按鈕有問題。我需要將它的高度設定為它的容器的比例,它是一個網格。我看到高度可以設定為一個值或“自動”,但我不知道如何對我的按鈕說他的高度必須是包含它的網格高度的 3/4。
有人知道如何處理嗎?可以用 XAML 做到這一點嗎?
目標是當網格增長時,按鈕也會隨之增長。
任何幫助將不勝感激。
謝謝
這是我寫的代碼:
<Window x:Class="WpfAppButtonSize.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfAppButtonSize"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid Height="100" Width="250" HorizontalAlignment="Center" VerticalAlignment="Center">
<Button Content="Button 1" FontSize="30" Height="75" Width="150"/>
</Grid>
</Grid>
對于按鈕,而不是Height="75",我想說Height=0.75 * Grid Height
uj5u.com熱心網友回復:
最簡單的方法是將按鈕放置在其自己的網格中,并適當調整行大小以生成間距。
例如
<Grid ...>
<Grid Grid.Row="???" Grid.Column="???" ...>
<Grid.RowDefinitions>
<RowDefinition Height="3*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button Grid.Row="0" ... />
</Grid>
</Grid>
uj5u.com熱心網友回復:
您可以使用轉換器并系結 Grid 的“ActualHeight”屬性來實作這一點。
<Button Height="{Binding ElementName="mygGrid" Path=ActualHeight, Converter={StaticResource percentageConverter}}"/>
uj5u.com熱心網友回復:
如果通過指定x:Name屬性為 Grid 和 Button 命名,則可以Height在 Grid 的大小更改時在后面的代碼中設定 Button。
XAML
<Grid x:Name="MyGrid">
<Button x:Name="MyButton" />
</Grid>
主視窗.xaml.cs
public MainWindow()
{
InitializeComponent();
MyGrid.PropertyChanged = (s,e) =>
{
if(e.PropertyName == nameof(MyGrid.Height))
{
MyButton.Height = MyGrid.Height * 0.75;
}
}
}
編輯:可能需要使用 Grid 的ActualHeight屬性而不是Height.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/524080.html
標籤:wpfxml按钮浆纱
下一篇:訪問嵌套的XAML元素
