Файл MainWindow.xaml
<Window x:Class="WpfAppChildWidthInPercent.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:WpfAppChildWidthInPercent"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<!-- my width converter -->
<Window.Resources>
<local:PercentageConverter x:Key="percentageConverter"/>
</Window.Resources>
<!-- stackpanel -->
<StackPanel x:Name="my1">
<!-- button -->
<Button Content="Hello!"
HorizontalAlignment="Left"
Width="{Binding
Converter={StaticResource percentageConverter},
ElementName=my1,
Path=ActualWidth,
ConverterParameter=0.4}"> <!-- 0.4 это значит что будет 40% от всего экрана, расчет написан в классе PercentageConverter -->
</Button>
</StackPanel>
</Window>
C#
Файл MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfAppChildWidthInPercent
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
// my converter
public class PercentageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return System.Convert.ToDouble(value) * System.Convert.ToDouble(parameter);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}