WPF:调整图像大小,但仅在用户调整UI大小时

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF:调整图像大小,但仅在用户调整UI大小时相关的知识,希望对你有一定的参考价值。

我有一个基于多个网格的WPF应用程序。到目前为止,它按预期工作。应用程序在顶部显示某种工具栏,在中心/底部显示Image控件。当我调整窗口大小时,Image元素也会调整大小,这非常好。

现在,当我将ImageSource设置为Image控件并且图像很大(例如垂直)时,这将导致整个窗口也垂直调整大小。

如何允许用户调整窗口大小(包括调整图像大小)但是当图像加载到Image控件时不允许调整大小操作?

<Window x:Class="GrafAoiKyErrorViewer.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:ignore="http://www.galasoft.ch/ignore"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:command="http://www.galasoft.ch/mvvmlight"
    xmlns:grafAoiKyErrorViewer="clr-namespace:GrafAoiKyErrorViewer"
    mc:Ignorable="d ignore"
    ResizeMode="CanResizeWithGrip"
    SizeToContent="WidthAndHeight"
    Title="Graf AOI KY Error Viewer"
    MinWidth="600"
    DataContext="Binding Main, Source=StaticResource Locator"
    Icon="Images/AnalyzeTrace_16x.png" MinHeight="800"
    >

<i:Interaction.Triggers>
    <i:EventTrigger EventName="Loaded">
        <command:EventToCommand Command="Binding WindowLoadedCommand" />
    </i:EventTrigger>
</i:Interaction.Triggers>

<Window.InputBindings>
    <KeyBinding Command="Binding Path=SelectNextCommand" Gesture="F2" />
    <KeyBinding Command="Binding Path=SelectPreviousCommand" Gesture="F1" />
</Window.InputBindings>

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Skins/MainSkin.xaml" />
        </ResourceDictionary.MergedDictionaries>
        <grafAoiKyErrorViewer:ErrorClassToStringConverter x:Key="ErrorClassToStringConverter" />
    </ResourceDictionary>
</Window.Resources>

<Grid Margin="10">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"></ColumnDefinition>
        <ColumnDefinition Width="Auto"></ColumnDefinition>
        <ColumnDefinition Width="*"></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Label Grid.Row="0" Grid.Column="0" Margin="3">Datenbank</Label>
    <ComboBox Grid.Row="0" Grid.Column="1" Margin="3" MinWidth="400" ItemsSource="Binding AoiDatabaseCollection" DisplayMemberPath="DisplayString" SelectedValue="Binding AoiDatabaseSelected" />
    <Label Grid.Row="1" Grid.Column="0" Margin="3">Projekt</Label>
    <ComboBox Grid.Row="1" Grid.Column="1" Margin="3" MinWidth="400" ItemsSource="Binding AoiProjectCollection" DisplayMemberPath="Name" SelectedValue="Binding AoiProjectSelected" />
    <Label Grid.Row="2" Grid.Column="0" Margin="3">Lot</Label>
    <ComboBox Grid.Row="2" Grid.Column="1" Margin="3" MinWidth="400" ItemsSource="Binding AoiLotCollection" DisplayMemberPath="Name" SelectedValue="Binding AoiLotSelected" />



    <Grid Margin="10" Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="3">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>

        <ListView Grid.Column="0" Grid.Row="0" Margin="3" ItemsSource="Binding ErrorClassCollection" Height="200" SelectionMode="Single" MinWidth="200">
            <ListView.View>
                <GridView>
                    <GridView.Columns>
                        <GridViewColumn>
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <CheckBox IsChecked="Binding IsSelected" />
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn DisplayMemberBinding="Binding Item, Converter=StaticResource ErrorClassToStringConverter" Header="Fehlerklasse" />
                    </GridView.Columns>
                </GridView>
            </ListView.View>
        </ListView>
        <StackPanel Orientation="Horizontal" Grid.Row="1" Grid.Column="0">
            <Button Margin="3" Padding="3" Command="Binding ModifySelectionErrorClassAllOnCommand">Alle Ein</Button>
            <Button Margin="3" Padding="3" Command="Binding ModifySelectionErrorClassAllOffCommand">Alle Aus</Button>
            <Button Margin="3" Padding="3" Command="Binding ModifySelectionErrorClassRealErrorsOnlyCommand">Echte Fehler</Button>
        </StackPanel>
        <ListView Grid.Column="1" Grid.Row="0" Margin="3" ItemsSource="Binding PositionCollection" Height="200" SelectionMode="Single" MinWidth="200">
            <ListView.View>
                <GridView>
                    <GridView.Columns>
                        <GridViewColumn>
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <CheckBox IsChecked="Binding IsSelected" />
                                    <!--<CheckBox IsChecked="Binding IsSelected" Command="Binding DataContext.FailureSelectionChangedCommand, RelativeSource=RelativeSource FindAncestor, AncestorType=x:Type ListView" />-->
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn DisplayMemberBinding="Binding Item" Header="Position" />
                    </GridView.Columns>
                </GridView>
            </ListView.View>
        </ListView>
        <StackPanel Orientation="Horizontal" Grid.Row="1" Grid.Column="1">
            <Button Margin="3" Padding="3" Command="Binding ModifySelectionPositionAllOnCommand">Alle Ein</Button>
            <Button Margin="3" Padding="3" Command="Binding ModifySelectionPositionAllOffCommand">Alle Aus</Button>
        </StackPanel>

        <ListView Grid.Column="2" Grid.Row="0"  Margin="3" ItemsSource="Binding ArticleCollection" Height="200" SelectionMode="Single" MinWidth="200">
            <ListView.View>
                <GridView>
                    <GridView.Columns>
                        <GridViewColumn>
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <CheckBox IsChecked="Binding IsSelected" />
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn DisplayMemberBinding="Binding Item" Header="Artikel" />
                    </GridView.Columns>
                </GridView>
            </ListView.View>
        </ListView>
        <StackPanel Orientation="Horizontal" Grid.Row="1" Grid.Column="2">
            <Button Margin="3" Padding="3" Command="Binding ModifySelectionArticleAllOnCommand">Alle Ein</Button>
            <Button Margin="3" Padding="3" Command="Binding ModifySelectionArticleAllOffCommand">Alle Aus</Button>
        </StackPanel>
        <ListView Grid.Column="3" Grid.Row="0" Margin="3" ItemsSource="Binding PartTypeCollection" Height="200" SelectionMode="Single" MinWidth="200">
            <ListView.View>
                <GridView>
                    <GridView.Columns>
                        <GridViewColumn>
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <CheckBox IsChecked="Binding IsSelected" />
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn DisplayMemberBinding="Binding Item" Header="Bauform" />
                    </GridView.Columns>
                </GridView>
            </ListView.View>
        </ListView>
        <StackPanel Orientation="Horizontal" Grid.Row="1" Grid.Column="3">
            <Button Margin="3" Padding="3" Command="Binding ModifySelectionPartTypeAllOnCommand">Alle Ein</Button>
            <Button Margin="3" Padding="3" Command="Binding ModifySelectionPartTypeAllOffCommand">Alle Aus</Button>
        </StackPanel>
    </Grid>
    <Button Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="3"  Margin="3" Padding="3" HorizontalAlignment="Left" Command="Binding FailureSelectionChangedCommand">Aktualisieren</Button>
    <Grid Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="3">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid MinWidth="200">
            <!-- DUMMY GRID -->
        </Grid>
        <Image Grid.Row="0" Grid.Column="1" Source="Binding CurrentFailureItem.KyAoiFailureImage.Image2DData" Stretch="Uniform" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Image>

        <!--<Viewbox HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        </Viewbox>-->
        <!--<Border BorderThickness="1" BorderBrush="Black" >
            </Border>-->
        <Grid Grid.Row="1" Grid.Column="1" HorizontalAlignment="Center">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"></ColumnDefinition>
                <ColumnDefinition Width="Auto"></ColumnDefinition>
                <ColumnDefinition Width="Auto"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Button Grid.Column="0" MinWidth="100" Margin="3" Padding="3" VerticalAlignment="Center" Command="Binding SelectPreviousCommand">Previous</Button>
            <TextBlock Grid.Column="1" Margin="3" Padding="3" HorizontalAlignment="Stretch" TextAlignment="Center">
                <Run Text="Binding SelectedFailureItemIndexDisplay, Mode=OneWay"></Run>
                <Run Text=" / "></Run>
                <Run Text="Binding TotalFailureItemIndex, Mode=OneWay"></Run>
            </TextBlock>
            <Button Grid.Column="2" MinWidth="100" Margin="3" Padding="3" VerticalAlignment="Center" Command="Binding SelectNextCommand">Next</Button>
        </Grid>
        <StackPanel Grid.Row="0" Grid.Column="2" Orientation="Vertical" VerticalAlignment="Center" Margin="3" MinWidth="200">
            <TextBlock Text="Binding CurrentFailureItem.Position" FontSize="10" FontWeight="Bold"></TextBlock>
            <TextBlock Text="Binding CurrentFailureItem.Article" FontSize="10" FontWeight="Bold"></TextBlock>
            <TextBlock Text="Binding CurrentFailureItem.PartType" FontSize="10" FontWeight="Bold"></TextBlock>
            <TextBlock Text="Binding CurrentFailureItem.ErrorClass" FontSize="10" FontWeight="Bold"></TextBlock>
        </StackPanel>

    </Grid>

</Grid>

答案

您所看到的实际上是控件的预期行为,但是要解决此问题,请在代码中找到以下行:

    <Grid Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="3" >

并将其替换为:

    <Grid Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="3" MaxHeight="100">

同时将MaxHeight设置为合理的值

编辑:好的,在这种情况下理解用网格替换图像并使用其背景:

            <Grid Grid.Row="0" Grid.Column="1">
               <Grid.Background>
                  <ImageBrush ImageSource="Binding CurrentFailureItem.KyAoiFailureImage.Image2DData" Stretch="Uniform"></ImageBrush>
               </Grid.Background>
            </Grid>
另一答案

尝试在Viewbox中嵌套Image控件:

<Viewbox> <Image ImageSource="Binding MyImage"/> </Viewbox>

你可能需要搞乱Viewbox Stretch属性(我总是忘记哪一个做了什么,但你通常可以通过一些试验和错误得到一个你想做的事)。

以上是关于WPF:调整图像大小,但仅在用户调整UI大小时的主要内容,如果未能解决你的问题,请参考以下文章

WPF Toolkit DataGrid 列调整大小事件

Direct2D,WPF D3D10 图像在调整大小时模糊

WPF调整大小问题

URL图片仅在调整浏览器大小时显示

加载时将调整大小功能绑定到图像

使用页面宽度调整大小更改图像