[英]how to give resize support to my custom control when explicitly set width and height in wpf
我想在調整主窗口大小時為我的自定義控件提供調整大小的行為,即使我明確設置寬度和高度也是如此。 我怎樣才能做到這一點?
這里只是簡單的代碼。 像這樣我的自定義控件。
<Border Background="Red" Width="300" Height="300" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" CornerRadius="15"/>
任何人都可以向我提供您的建議。
只需不給此控件明確的任何Height和Width即可。 並將其放在具有RowDefinition * Height和ColumnDefinition With * Width的窗口網格中。
用戶控件
<UserControl x:Class="debuggingusingreflector.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
>
<Grid>
<TextBox Background="Gray"/>
</Grid>
Window.xaml
<Window x:Class="debuggingusingreflector.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:x1="clr-namespace:debuggingusingreflector"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBox Height="100" Text="{Binding Name}" Background="Red"/>
<x1:UserControl1 Grid.Row="1"/>
</Grid>
我希望這將有所幫助。
您可以在運行時通過代碼影響Width
/ Height
屬性。 調整主窗體的大小時,執行這些操作也沒有問題。 樣品:
XAML
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded" SizeChanged="Window_resize">
<Grid>
<Border Background="Red" Width="300" Height="300" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" CornerRadius="15" Name="borderName" />
</Grid>
</Window>
CS
private void Window_resize(Object sender, SizeChangedEventArgs e)
{
borderName.Width = 0.5 * this.Width;
borderName.Height = 0.5 * this.Height;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.