ContentPresenter未显示UserControl中的内容

时间:2013-12-20 18:48:47

标签: c# wpf xaml user-controls

我有一个名为SelectScreenShots的窗口,其中包含一个Button和一个ContentPresenter。我想通过用户控件在Content Presenter中显示图像和按钮列表,但是当我加载程序时,没有任何显示。我想我的绑定可能不对,但我不知道在哪里。有没有人看到我失踪的小东西,或者我只是做了一些完全错误的事情?

SelectScreenShots xaml和后面的代码:

<Window x:Class="Client.App.Support.SelectScreenShots"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    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="550" Width="800">
<Window.CommandBindings>
</Window.CommandBindings>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="30"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Button Name="_back" Click="_back_Click">Back</Button>
    <ContentPresenter Grid.Row="1" Name="_contentPresenter" DataContext="{Binding ''}"/>
</Grid>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Client.App.Support
{
    /// <summary>
    /// Interaction logic for SelectScreenShots.xaml
    /// </summary>
    public partial class SelectScreenShots : Window
    {
        public SelectScreenShots()
        {
            InitializeComponent();
            ListOfScreenShots loss = new ListOfScreenShots();
            this._contentPresenter.DataContext = loss._itemsControl;
        }

        private void _back_Click(object sender, RoutedEventArgs e)
        {
        }
    }
}

这是我的UserControl背后的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" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
        <ItemsControl Name="_itemsControl" ItemsSource="{Binding ''}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="550"/>
                            <ColumnDefinition Width="250"/>
                        </Grid.ColumnDefinitions>
                        <Image Name="_image" Grid.Column="0" Height="400" Width="550" HorizontalAlignment="Center" VerticalAlignment ="Center" Stretch="Uniform" Source="{Binding ''}"/>
                        <Grid Grid.Column="1">
                            <Grid.RowDefinitions>
                                <RowDefinition/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>
                            <Button Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Bottom" Name="_addscreenshot" Content="Select Screenshot" 
                                    Height="30" Width="150" Margin="3.5,0,3.5,7"/>
                            <Button Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Top" Name="_removescreenshot" Content="Remove Screenshot" 
                                    Height="30" Width="150" Margin="3.5,0,3.5,7"/>
                        </Grid>
                    </Grid>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </ScrollViewer>
</Grid>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Client.App.Support
{
    /// <summary>
    /// Interaction logic for ListOfScreenShots.xaml
    /// </summary>
    public partial class ListOfScreenShots : UserControl
    {
        public ListOfScreenShots()
        {
            InitializeComponent();
            this._itemsControl.ItemsSource = RenderWindows();
        }

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

}

1 个答案:

答案 0 :(得分:0)

我发现我做错了,这是一个愚蠢的错误。在SelectScreenShots背后的代码中

这样:

this._contentPresenter.DataContext = loss._itemsControl;

应该是这样的:

this._contentPresenter.Content = loss._itemsControl;