我在XAML中设置了一个StackPanel,其Orientation =“Horizontal”。 当我向它的儿童添加项目时,它们不会以预先设定的大小进行渲染。 我正在添加的项是从Canvas派生的类,其中Image(Stretch = Uniform)和垂直StackPanel中的TextBlock是唯一的子项。我明确设置了画布的宽度和高度,我可以在调试时看到正确的值,但它们渲染得更大(看起来像拉伸以适合?)。 我做错了什么?
下面的代码 - 抱歉格式化,但尝试用代码引号括起来似乎不适用于我的Mac!
<Border BorderThickness="4" BorderBrush="DarkGray" Grid.Row="0"
CornerRadius="0, 12, 0, 0">
<ScrollViewer HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Disabled" x:Name="navScroller">
<StackPanel x:Name="navPanel" Background="White" Orientation="Horizontal">
<Button Style="{StaticResource GlassButton}" Height="30"
HorizontalAlignment="Left" Name="homeBtn"
VerticalAlignment="Center" Width="30"
Click="HomeBtn_Click" Margin="4,4,4,4" >
<Image Source="{StaticResource homeImg}"></Image>
</Button>
</StackPanel>
</ScrollViewer>
</Border>
从Canvas派生的类: -
public IconView(string filePath, double sze)
{
this._myPath = filePath;
this.IsDir = isDir;
mySize = sze;
//Trace.WriteLine(_myPath);
string foldername = System.IO.Path.GetFileName(FilePath);
TextBlock tb = new TextBlock();
tb.Text = foldername;
tb.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
tb.VerticalAlignment = System.Windows.VerticalAlignment.Top;
StackPanel sp = new StackPanel();
sp.Orientation = Orientation.Vertical;
sp.Children.Add(GetThumbnail());
sp.Children.Add(tb);
this.Children.Add(sp);
this.Width = mySize;
this.Height = mySize * 1.4;
this.PreviewMouseUp += IconView_PreviewMouseUp;
this.PreviewTouchUp += IconView_PreviewTouchUp;
this.Margin = new Thickness(10);
}
添加到StackPanel: -
IconView iv = new IconView(path, 20);
Trace.WriteLine("iv size = " + iv.Width + " x " + iv.Height);
iv.VerticalAlignment = System.Windows.VerticalAlignment.Top;
iv.Height = 20;
iv.Width = 20;
iv.Margin = new Thickness(20,10,20,10);
navPanel.Children.Add(iv);