簡體   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