c# - controles - custom control wpf
ContentPresenter no muestra el contenido de mi UserControl (1)
Tengo una ventana llamada SelectScreenShots que contiene un botón y un ContentPresenter. Quiero mostrar una lista de imágenes y botones en ese Presentador de contenido a través de un control de usuario, pero cuando cargo el programa, no aparece nada. Creo que puedo estar haciendo mal mis enlaces, pero no sé dónde. ¿Alguien ve algo pequeño que me falta, o simplemente estoy haciendo algo completamente incorrecto?
SelectScreenShots xaml y código detrás:
<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)
{
}
}
}
Y aquí está el xaml y el código detrás de mi UserControl:
<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;
}
}
}
Descubrí que estaba haciendo mal y fue un error estúpido. En el código detrás de SelectScreenShots
esta:
this._contentPresenter.DataContext = loss._itemsControl;
debería ser esto:
this._contentPresenter.Content = loss._itemsControl;