繁体   English   中英

如何在 .net core WPF 项目的 XAML 中设置图像的“高度”或“宽度”

[英]How to set the "Height" or "Width" of an image in XAML of a .net core WPF project

我正在使用 AvalonDock(作为 Docking 解决方案),并且它的“avalonDock:LayoutAnchorable.IconSource”属性需要一个 ImageSource。

我有这个代码:

        <avalonDock:LayoutRoot.LeftSide>
            <avalonDock:LayoutAnchorSide>
                <avalonDock:LayoutAnchorGroup>
                    <!--IconSource="pack://application:,,,/PtdcGui;component/Assets/Image/Scope.png"-->

                    <avalonDock:LayoutAnchorable
                        Title="AutoHide1 Content"
                        ContentId="AutoHide1Content">
                        <avalonDock:LayoutAnchorable.IconSource>
                            <BitmapImage DecodePixelHeight="20"
                                UriSource="pack://application:,,,/PtdcGui;component/Assets/Image/Scope.png">
                            </BitmapImage>
                        </avalonDock:LayoutAnchorable.IconSource>
                        <TextBox Text="{Binding TestTimer, Mode=OneWay, StringFormat='AutoHide Attached to Timer ->\{0\}'}" />
                    </avalonDock:LayoutAnchorable>
                    <avalonDock:LayoutAnchorable Title="AutoHide2 Content" ContentId="AutoHide2Content">
                        <StackPanel Orientation="Vertical">
                            <TextBox />
                            <TextBox />
                        </StackPanel>
                    </avalonDock:LayoutAnchorable>
                </avalonDock:LayoutAnchorGroup>
            </avalonDock:LayoutAnchorSide>
        </avalonDock:LayoutRoot.LeftSide>

20 像素高的图标出现在屏幕上,高度约为 32 像素:

出现在屏幕上:Scope.png 原来的: 范围.png

如何将图标大小调整为 20 的固定高度?

注意:在 .net core 中,听起来我们应该使用 BitmapImage,它在 xaml 中没有可用的“高度”(或“宽度”)属性。

我发现的最简单的方法是将图标放在网格中,然后您可以调整网格的大小以更改图像的大小。

例如:

 <Grid Grid.Row="1" Grid.Column="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="25"/>
                <RowDefinition Height="5"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            
            <TextBlock Grid.Row="0" Name="TitleTextBlock" Text="Budgets" FontWeight="Bold"/>
            <Image Grid.Row="0" Name="SettingsImage" HorizontalAlignment="Right"></Image>
</Grid>

然后在代码隐藏中,您可以执行以下操作:

var icon = new PackIconFontAwesome() {Kind = PackIconFontAwesomeKind.AsteriskSolid};
Geometry geo = Geometry.Parse(icon.Data);
GeometryDrawing gd = new GeometryDrawing();
gd.Geometry = geo;
gd.Brush = icon.BorderBrush;
gd.Pen = new Pen(Brushes.White, 8);
DrawingImage geoImage = new DrawingImage(gd);
geoImage.Freeze();

SettingsImage.Source = geoImage;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM