背景:
我正在为设置页面生成 UI。这些设置存储在字典中,因为每个相关对象的设置都不同。
问题:
的ScrollableHeight
aScrollViewer
对内容的大小不准确。当内容ScrollViewer
更改时,ScrollableHeight
不会重置,而是附加新内容的高度。
什么时候:
我在 a 中生成内容Grid
,这是ScrollViewer
. 内容是RowDefinitions
名称-值对显示为TextBlocks
和的位置TextBoxes
。When a different object is selected in order to edit its properties, the Grid's Children are cleared and the UI to display the properties is regenerated. 正如我之前在问题定义中提到的,生成的内容的高度附加到ScrollViewer
'ScrollableHeight
属性中。
我学到:
我的第一个想法是清除ScrollViewer
'sScrollableHeight
并为每一行添加附加行的高度以达到正确的大小。问题是ScrollableHeight
无法设置(私人二传手)。
代码:
XAML:
<ScrollViewer Name="svScroller" Grid.Row="0">
<Grid x:Name="gdPropertyGrid" Margin="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="35*" />
<ColumnDefinition Width="65*" />
</Grid.ColumnDefinitions>
</Grid>
</ScrollViewer>
C#:
//Get Selected Item
var listBox = ((ListBox)sender);
var provider = listBox.SelectedItem as IProviderConfiguration;
if (provider != null)
{
tbTitle.Text = String.Format("Properties for {0}",provider.Name);
int rowCount = 0;
PropertyGrid.Children.Clear();
//Get properties
foreach (var property in provider.Properties)
{
//Create Grid Row
var rowDef = new RowDefinition() {Height = new GridLength(30)};
PropertyGrid.RowDefinitions.Add(rowDef);
//Create Name Label
var tbPropertyName = new TextBlock {
Text = property.Key,
VerticalAlignment = VerticalAlignment.Center
};
//Create Value input
var tbPropertyValue = new TextBox {
Text = property.Value.ToString(),
VerticalAlignment = VerticalAlignment.Center
};
//Add TextBlock & TextBox Grid
PropertyGrid.Children.Add(tbPropertyName);
PropertyGrid.Children.Add(tbPropertyValue);
//Set Grid.Row Attached property
Grid.SetRow(tbPropertyName, rowCount);
Grid.SetRow(tbPropertyValue, rowCount);
Grid.SetColumn(tbPropertyValue, 1);
rowCount++;
}
}