Один из способов добиться этого - привязать все свойства Width/Height
к одному и тому же свойству ViewModel.Вот пример:
ViewModel:
public class ViewModel : INotifyPropertyChanged
{
private int width, height;
public int Width { get { return width; } set { width = value; RisePropertyChanged(); } }
public int Height { get { return height; } set { height = value; RisePropertyChanged(); } }
public event PropertyChangedEventHandler PropertyChanged;
protected void RisePropertyChanged([CallerMemberName] string propertyName = null)
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
View:
<Window x:Class="WpfApplication2.MainWindow"
<!-- ... -->
Height="{Binding Height, Mode=OneWayToSource}"
Width="{Binding Width, Mode=OneWayToSource}">
<Grid>
<Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top"
Margin="10,10,0,0" Height="{Binding Height}" Width="{Binding Width}"/>
<Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top"
Margin="300,10,0,0" Height="{Binding Height}" Width="{Binding Width}"/>
<Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top"
Margin="10,180,0,0" Height="{Binding Height}" Width="{Binding Width}"/>
<Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top"
Margin="300,90,0,0" Height="{Binding Height}" Width="{Binding Width}"/>
</Grid>
</Window>
Просмотреть код позади:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
}
Теперь размер кнопокбудет отслеживаться как размер вашей формы, включая изменение размера формы.Повторюсь, это не идеальное решение, но оно может привести вас к одному.