我無法調整WPF控件的大小(我將在此示例中使用Grid
控件)與Window
的Client
區域的大小有關。我意識到Grid
默認會自動填充所有可用空間,但是我需要手動設置Grid
的Width
,以便我可以將其從另一個控件綁定到它(如果DataGrid的其中一列Width
的設置爲Star
if它有什麼區別)。填充內容以填充窗口的「客戶區」
請考慮下面的XAML:
<Window x:Class="TestApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="768" Width="1024"
x:Name="mainWindow">
<ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Auto">
<Grid x:Name="testGrid" Background="DarkGray">
</Grid>
</ScrollViewer>
</Window>
而下面的代碼隱藏:
using System.Windows;
namespace TestApplication
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
testGrid.Width = mainWindow.Width - (SystemParameters.BorderWidth * 2);
}
}
}
當運行這個簡單的應用程序,該Grid
的寬度比Client
面積稍大的Window
,導致顯示Horizontal Scroll Bar
。如果SystemParameters.BorderWidth
沒有準確計算出Window
邊框的寬度,會發生什麼?
根據最新的MSDN文檔,SystemParameters.BorderWidth
:獲取確定非最小化窗口的非客戶端區域邊界寬度的度量標準。
+1,因爲它有助於'ScrollViewer';不幸的是,['ViewportWidth'](http://msdn.microsoft.com/en-us/library/system.windows.controls.scrollcontentpresenter.viewportwidth%28v=vs.110%29.aspx)和['ViewportHeight'] (http://msdn.microsoft.com/en-us/library/system.windows.controls.scrollcontentpresenter.viewportheight%28v=vs.110%29.aspx)似乎不是['ScrollContentPresenter']上的依賴項屬性(http://msdn.microsoft.com/en-us/library/system.windows.controls.scrollcontentpresenter%28v=vs.110%29.aspx)。 –