简体   繁体   English

为什么我的图像没有显示在UserControl中?

[英]Why are my images not showing up in my UserControl?

I have a ContentPresenter that displays a UserControl . 我有一个ContentPresenter ,它显示UserControl The UserControl contains an ItemsControl that binds to a list of images. UserControl包含一个绑定到图像列表的ItemsControl When I change the DataContext for my UserControl the Images are there, they just aren't displaying. 当我为UserControl更改DataContext ,图像在那里,它们只是不显示。 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. 这是一些代码,可让您更好地了解正在使用的工具。

Edit: 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. ListOfScreenShots是另一个显示图像和按钮的用户控件,允许用户选择图像。 As screen shots are selected they are added to a List<BitmapSource> and they are supposed to display in EnlargedScreenShots . 选择了屏幕快照后,它们便被添加到List<BitmapSource> ,并应显示在EnlargedScreenShots Here is how the UserControl is applied 这是UserControl的应用方式

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; } }

Here is the xaml for ListOfScreenShots . 这是ListOfScreenShots的xaml。 That should also give a clearer understanding of what is going on. 这也应该使您对正在发生的事情有更清晰的了解。

ListOfScreenShots xaml: 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>

Main Window xaml: 主窗口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>

Main Window code behind: 主窗口后面的代码:

    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();
    }
}
}

User Control xaml: 用户控件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. 现在,这真的困扰着我。

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. 我想这与以下事实有关:我将所有内容包装在堆栈面板中,但是在将DataTemplate内部的图像包装在StackPanel ,它仍然无法正常工作。 I also tried removing the StackPanel altogether and just using the WrapPanel . 我还尝试过完全删除StackPanel并仅使用WrapPanel

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thank you. 谢谢。

Too many things going on to give a definitive answer but one thing that immediately springs to mind is you aren't doing property change notification. 太多的事情不能给出确切的答案,但立即想到的一件事是您没有在进行财产变更通知。 Try switching your lists to ObservableCollection and read this MSDN article on the subject . 尝试将列表切换到ObservableCollection,并阅读有关该主题的MSDN文章

My guesses: 我的猜测:

1) your UserControl is sized as 0;0 so you won't see anything. 1)您的UserControl的大小为0; 0,因此您将看不到任何内容。 Bind UserControl Width/Height to ContentPresenter Width/Height, as so: 将UserControl宽度/高度绑定到ContentPresenter宽度/高度,如下所示:

<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" 
         Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type ContentPresenter}}}"
         Height="{Binding Path=ActualHeight, RelativeSource={RelativeSource AncestorType={x:Type ContentPresenter}}}"
         d:DesignHeight="162" d:DesignWidth="800">

2) Add Width/Height properties to Image element, set it explictly for testing. 2)将“宽度/高度”属性添加到“图像”元素,并对其进行显式设置以进行测试。 Also add TextBlock and bind Text against Image.Source to see if bindings work finely. 还要添加TextBlock并将Text绑定到Image.Source上,以查看绑定是否工作正常。

This problem needs help of Snoop(WPF Spy utility) that will answer this question in matter of minutes, so perhaps this is the most useful suggestion for now: http://snoopwpf.codeplex.com/ 此问题需要Snoop(WPF Spy实用程序)的帮助,该问题将在几分钟内解决此问题,因此,这也许是目前最有用的建议: http : //snoopwpf.codeplex.com/

Ps you are sharing the same BitmapSource between two controls. ps,您在两个控件之间共享相同的BitmapSource。 I don't exactly know, but this might be the fishy part too. 我不完全知道,但这也可能是可疑的部分。 So if you can, try duplicating it. 因此,如果可以,请尝试将其复制。

I was doing something very obviously wrong in my code behind. 我在后面的代码中犯了很明显的错误。

I wasn't instantiating a new instance of EnlargedScreenShot when my command was executed. 执行命令时,我没有实例化EnlargedScreenShot的新实例。 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();

Into here: 进入这里:

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;
    }
}

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

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