为什么我的图像没有显示在我的 UserControl 中? [英] Why are my images not showing up in my UserControl?

查看:25
本文介绍了为什么我的图像没有显示在我的 UserControl 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示 UserControlContentPresenter.UserControl 包含一个绑定到图像列表的 ItemsControl.当我为我的 UserControl 更改 DataContext 时,图像在那里,它们只是不显示.我认为正在发生的事情是它们只是堆叠在一起而不是包裹,但我不知道为什么.下面是一些代码,可以让您更好地了解我正在使用的内容.

I have a ContentPresenter that displays a UserControl. The UserControl contains an ItemsControl that binds to a list of images. When I change the DataContext for my UserControl the Images are there, they just aren't displaying. I think what is happening is they are just stacking on top of each other instead of wrapping, but I don't know why. Here is some code to give you a better idea of what I am working with.

为了更好地了解这里的程序如何流动是正在发生的事情.ListOfScreenShots 是另一个显示图像和按钮的用户控件,允许用户选择图像.当屏幕截图被选择时,它们被添加到 List 并且它们应该显示在 EnlargedScreenShots 中.下面是 UserControl 是如何应用的

To get a better of idea of how the program flows here is what is going on. ListOfScreenShots is another user control that displays images and buttons allowing users to select the images. As screen shots are selected they are added to a List<BitmapSource> and they are supposed to display in EnlargedScreenShots. Here is how the UserControl is applied

private void MainWindowCommandBinding_Executed(object sender, ExecutedRoutedEventArgs e){if (e.Command == SelectImageCommand){selectedImages.Add(e.Parameter as BitmapSource);ess.DataContext = selectedImages;this._contentPresenter2.Content = ess;}}

这是 ListOfScreenShots 的 xaml.这也应该能让您更清楚地了解正在发生的事情.

Here is the xaml for ListOfScreenShots. That should also give a clearer understanding of what is going on.

ListOfScreenShots xaml:

    <UserControl x:Class="Client.App.Support.ListOfScreenShots"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d"
             xmlns:support="clr-namespace:Client.App.Support"
             d:DesignHeight="400" d:DesignWidth="800">
    <Grid>
        <ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
            <ItemsControl Name="_listBox" ItemsSource="{Binding ''}">
                <!--<ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel Orientation="Vertical"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>-->
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Image Name ="_thumbnailImage" Width="600" VerticalAlignment="Center" Source="{Binding ''}"/>
                            <Button VerticalAlignment="Center" HorizontalAlignment="Center" Name="_addscreenshot" Content="Select Screenshot" Command="{x:Static support:SelectScreenShots.SelectImageCommand}" 
                                    CommandParameter="{Binding ''}" Width="150" />
                        </StackPanel>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </ScrollViewer>
    </Grid>
</UserControl>

主窗口 xaml:

<dxc:DXWindow x:Class="Client.App.Support.SelectScreenShots"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/core" Focusable="False" IsTabStop="False"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
    xmlns:libRes="clr-namespace:Shared.Lib.Resources;assembly=Shared.Lib"
    xmlns:support="clr-namespace:Client.App.Support"
    Title="Select Images" Height="600" Width="800">

<Window.CommandBindings>
    <CommandBinding Command="support:SelectScreenShots.SelectImageCommand" Executed="MainWindowCommandBinding_Executed"/>
</Window.CommandBindings>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="367"/>
            <RowDefinition Height="167"/>
            <RowDefinition Height="33"/>
        </Grid.RowDefinitions>
        <ContentPresenter Grid.Row="0" Name="_contentPresenter" Content="{Binding ''}"/>
        <ContentPresenter Grid.Row="1" Name="_contentPresenter2" Content="{Binding ''}"/>
        <StackPanel Grid.Row="2" HorizontalAlignment="Right"  Orientation="Horizontal">
            <Button Name="_OK_Button" Content="OK" Click="_OK_Button_Click" Margin="0,5,5,5" Width="75" Height="23"/>
            <Button Name="_Cancel_Button" Content="Cancel" Click="_Cancel_Button_Click" Margin="0,5,5,5" Width="75" Height="23"/>
        </StackPanel>
    </Grid>

后面的主窗口代码:

    public partial class SelectScreenShots : DXWindow
{
    public static readonly RoutedCommand SelectImageCommand = new RoutedCommand();

    public static List<BitmapSource> selectedImages = new List<BitmapSource>();
    public static List<BitmapSource> screenshots = new List<BitmapSource>();
    public static ListOfScreenShots loss = new ListOfScreenShots();
    public static EnlargedScreenShot ess = new EnlargedScreenShot();

    public SelectScreenShots()
    {
        Client.GUI.AppGUI.SetupTheme(this);
        InitializeComponent();
        screenshots = RenderWindows();
        loss.DataContext = screenshots;
        this._contentPresenter.Content = loss;
    }

    public static List<BitmapSource> RenderWindows()
    {
        var windows = Application.Current.Windows
                                         .OfType<Window>()
                                         .Where(x => x.GetType() != typeof(AskAQuestionDialog) & x.GetType() != typeof(SelectScreenShots));

        var bitmaps = new List<BitmapSource>();

        foreach (var window in windows)
        {
            var bitmap = new RenderTargetBitmap((int)window.Width, (int)window.Height, 96d, 96d, PixelFormats.Default);
            bitmap.Render(window);

            bitmaps.Add(bitmap);
        }

        return bitmaps;
    }


    private void MainWindowCommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        if (e.Command == SelectImageCommand)
        {
            selectedImages.Add(e.Parameter as BitmapSource);
            ess.DataContext = selectedImages;
            this._contentPresenter2.Content = ess;
        }
    }


    private void _OK_Button_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
    }

    private void _Cancel_Button_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
    }
}
}

用户控制 xaml:

<UserControl x:Class="Client.App.Support.EnlargedScreenShot"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="162" d:DesignWidth="800">
<Grid>
    <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled">
        <StackPanel Orientation="Horizontal">
            <ItemsControl Name="_itemsControl" ItemsSource="{Binding ''}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                        <Image Name ="_thumbnailImage" HorizontalAlignment="Left" VerticalAlignment="Center" Source="{Binding ''}"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
        </StackPanel>
    </ScrollViewer>
</Grid>

在调整大小和滚动条可见性方面还有一些其他问题无法正常工作,但我稍后会解决这些问题.现在这真的让我很烦恼.

There are some other things going on as far as sizing and scroll bar visibility not working properly, but I will work those out later. Right now this is really bugging me.

任何人都看到任何明显错误的东西.我猜这与我将所有内容都包裹在堆栈面板中的事实有关,但在我将 DataTemplate 中的图像包裹在 StackPanel 之前> 还是不行.我还尝试完全删除 StackPanel 并仅使用 WrapPanel.

Anyone see anything obviously wrong. I am going to guess it has something to do with the fact that I have everything wrapped in a stack panel, but before I just had the image inside the DataTemplate wrapped in a StackPanel and it still didn't work. I also tried removing the StackPanel altogether and just using the WrapPanel.

任何帮助将不胜感激.谢谢.

Any help would be greatly appreciated. Thank you.

推荐答案

我在后面的代码中做了一些非常明显的错误.

I was doing something very obviously wrong in my code behind.

在执行我的命令时,我没有实例化 EnlargedScreenShot 的新实例.当窗口加载时它被实例化一次,然后我没有更新它.我只好搬家

I wasn't instantiating a new instance of EnlargedScreenShot when my command was executed. It was being instantiated once, when the window loaded and then I wasn't updating it. I just had to move

EnlargedScreenShot ess = new EnlargedScreenShot();

进入:

private void MainWindowCommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
    EnlargedScreenShot ess = new EnlargedScreenShot();
    if (e.Command == SelectImageCommand)
    {
        selectedImages.Add(e.Parameter as BitmapSource);
        ess.DataContext = selectedImages;
        this._contentPresenter2.Content = ess;
    }
}

这篇关于为什么我的图像没有显示在我的 UserControl 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆