我无法想出一种在Xaml中严格执行此操作的方法。 您可以使用MultiBinding作为MaxWidth的 TextBox 在哪里使用以下作为绑定
TextBox
Grid
Button
ProgressBar
的 XAML 强>
<Grid Name="grid"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Button Name="button" Grid.Column="0" Content="Cancel"/> <TextBox Grid.Column="1" Text="Some Text"> <TextBox.MaxWidth> <MultiBinding Converter="{StaticResource MaxWidthMultiConverter}"> <Binding ElementName="grid" Path="ActualWidth"/> <Binding ElementName="button" Path="ActualWidth"/> <Binding ElementName="progressBar" Path="MinWidth"/> </MultiBinding> </TextBox.MaxWidth> </TextBox> <ProgressBar Name="progressBar" Grid.Column="2" Value="50" Maximum="100" MinWidth="200"/> </Grid>
的 MaxWidthMultiConverter 强>
public class MaxWidthMultiConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) { double gridWidth = (double)values[0]; double buttonWidth = (double)values[1]; double progressBarMaxWidth = (double)values[2]; return gridWidth - (buttonWidth + progressBarMaxWidth); } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } }