There seems to be a change in behaviour between .Net 3.5 and .Net 4.0.
If I define a window as:
<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="500" >
<Grid>
<Grid.ColumnDefinitio开发者_如何转开发ns>
<ColumnDefinition MinWidth="300" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBox Grid.Column="1" ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Auto" Text="abc abc abc abc abc abc abc abc abcabc abc abcabc abc abc abc abc abc" />
</Grid>
</Window>
In .Net 3.5 the textbox correctly contains itself within the grid cell but in .Net 4.0 it extends beyond the cell and so gets clipped. This only happens if the MinWidth of the first column is greater than 50% of the overall width.
Does anyone know how to get 4.0 to exhibit the same behavior as 3.5?
After thinking about it for a bit I think I have a acceptable workaround. I'm not completely happy with it and if anyone has a better (cleaner!) solution then I would really like to know.
The workaround relies on the fact that the correct behaviour is exhibited as long as the Width property is set for the column (not just the MinWidth). So the answer is to always set the Width property (via binding) to be the correct percentage of the Grid (basically what WPF should be doing for me).
To do this first create a Converter that can do multiplication:
public class MultiplicationConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null) return double.NaN;
return (double)value * double.Parse((string)parameter);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Then calculate what percentage of the grid the MinWidth column is:
MinWidth of column / Total Width of Grid = percentage
Min Width = 300 Total Width = 478
300 / 478 = 62.7%
Then change the Xaml to set the Width of the MinWidth column to the percentage of the grid (note that the namespace for the converter would change to which ever namespace you put it into):
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="500" >
<Window.Resources>
<local:MultiplicationConverter x:Key="MultiplicationConverter" />
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="300" Width="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type Grid}}, Converter={StaticResource MultiplicationConverter}, ConverterParameter=0.627}" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBox Grid.Column="1" ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Auto" Text="abc abc abc abc abc abc abc abc abcabc abc abcabc abc abc abc abc abc" />
</Grid>
</Window>
I'm going to leave this "unanswered" for a few days to see if anyone can come up with a cleaner solution (the solution above really is a hack!).
精彩评论