Вам нужно установить MaxWidth
и MaxHeight
на ViewBox
:
<Grid>
<Viewbox x:Name="MyViewBox">
<TextBox x:Name="MyTextBox" Text="Hello world" />
</Viewbox>
</Grid>
и в код :
MyViewBox.MaxWidth = MyTextBox.ActualWidth * 2d;
MyViewBox.MaxHeight = MyTextBox.ActualHeight * 2d;
Или, может быть, лучшее решение с преобразователем:
public class ViewBoxMaxWidthOrHeightConverter : IValueConverter
{
public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
double textBoxWidthOrHeight = (double) value;
return textBoxWidthOrHeight*2d;
}
public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new System.NotImplementedException();
}
}
и XAML:
<local:ViewBoxMaxWidthOrHeightConverter x:Key="ViewBoxMaxWidthOrHeightConverter"/>
<Grid>
<Viewbox x:Name="MyViewBox"
MaxWidth="{Binding ElementName=MyTextBox, Path=ActualWidth, Converter={StaticResource ViewBoxMaxWidthOrHeightConverter}, Mode=OneWay}"
MaxHeight="{Binding ElementName=MyTextBox, Path=ActualHeight, Converter={StaticResource ViewBoxMaxWidthOrHeightConverter}, Mode=OneWay}">
<TextBox x:Name="MyTextBox" Text="Hello world" />
</Viewbox>
</Grid>
Спасибо :-). Это было элегантное и легкое решение. – PEK