我知道这有点晚了,但我刚刚遇到了这个问题,这是我的解决方案。不幸的是,它不够通用,它仅适用于具有两列的网格,但它可能可以进一步适应。但是,它解决了所描述的问题和我自己的问题,所以这里是:
该解决方案包含黑客或解决方法,但是您想调用它。不是为左列和右列都声明 MinWidth,而是为第一列声明 MinWidth 和 MaxWidth。这意味着 GridSplitter 不会向右移动定义的位置。到目前为止,一切都很好。
下一个问题是,如果我们有一个可调整大小的容器(在我的例子中是窗口),这还不够。这意味着我们不能随心所欲地扩大左列,即使第二列可能有足够的空间。幸运的是,有一个解决方案:绑定 Grid ActualWidth 并使用加法转换器。转换器参数实际上是右列所需的 MinWidth,显然是负值,因为我们需要从 Grid Width 中减去它。您也可以使用 SubtractConvertor,但这取决于您。
这是xaml和代码:
<Grid Background="{DynamicResource MainBackground}" x:Name="MainGrid" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200" MinWidth="100" MaxWidth="{Binding Path=ActualWidth, RelativeSource={RelativeSource AncestorType=Grid}, Converter={Converters:AdditionConverter}, ConverterParameter=-250}" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<GridSplitter Width="3" VerticalAlignment="Stretch" Grid.Column="0"/>
<!-- your content goes here -->
</Grid>
和转换器:
[ValueConversion(typeof(double), typeof(double))]
public class AdditionConverter : MarkupExtension, IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
double dParameter;
if (targetType != typeof(double) ||
!double.TryParse((string)parameter, NumberStyles.Any, CultureInfo.InvariantCulture, out dParameter))
{
throw new InvalidOperationException("Value and parameter passed must be of type double");
}
var dValue = (double)value;
return dValue + dParameter;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
#endregion
#region Overrides of MarkupExtension
/// <summary>
/// When implemented in a derived class, returns an object that is set as the value of the target property for this markup extension.
/// </summary>
/// <returns>
/// The object value to set on the property where the extension is applied.
/// </returns>
/// <param name="serviceProvider">Object that can provide services for the markup extension.
/// </param>
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
#endregion
}
我希望这有帮助,
米海Drebot