C# ContentPresenter未显示来自my UserControl的内容
我有一个名为SelectScreenShots的窗口,其中包含一个按钮和一个ContentPresenter。我想通过用户控件显示该内容演示器中的图像和按钮列表,但当我加载该程序时,什么也没有显示。我想我可能做错了绑定,但我不知道在哪里。有没有人看到我遗漏了什么,或者我只是做错了什么 选择屏幕截图xaml和代码隐藏:C# ContentPresenter未显示来自my UserControl的内容,c#,wpf,xaml,user-controls,C#,Wpf,Xaml,User Controls,我有一个名为SelectScreenShots的窗口,其中包含一个按钮和一个ContentPresenter。我想通过用户控件显示该内容演示器中的图像和按钮列表,但当我加载该程序时,什么也没有显示。我想我可能做错了绑定,但我不知道在哪里。有没有人看到我遗漏了什么,或者我只是做错了什么 选择屏幕截图xaml和代码隐藏: <Window x:Class="Client.App.Support.SelectScreenShots" xmlns="http://schemas.micros
<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>
返回
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Shapes;
命名空间Client.App.Support
{
///
///SelectScreenShots.xaml的交互逻辑
///
公共部分类选择屏幕截图:窗口
{
公共屏幕截图()
{
初始化组件();
ListofScrenshots损失=新ListofScrenshots();
这。_contentPresenter.DataContext=loss。_itemsControl;
}
私有无效\u返回\u单击(对象发送方,路由目标)
{
}
}
}
下面是我的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>
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
命名空间Client.App.Support
{
///
///ListOfScreenShots.xaml的交互逻辑
///
Screenshots的公共部分类列表:UserControl
{
锡林肖特的公开列表()
{
初始化组件();
这是。_itemsControl.ItemsSource=RenderWindows();
}
公共静态列表RenderWindows()
{
var windows=Application.Current.windows
第()类
其中(x=>x.GetType()!=typeof(AskAQuestionDialog)&x.GetType()!=typeof(SelectScreenShots));
变量位图=新列表();
foreach(windows中的var窗口)
{
var bitmap=newrenderTargetBitmap((int)window.Width,(int)window.Height,96d,96d,PixelFormats.Default);
渲染(窗口);
位图。添加(位图);
}
返回位图;
}
}
}我发现自己做错了,这是一个愚蠢的错误。在“代码隐藏”中选择屏幕截图 这:
this.\u contentPresenter.DataContext=loss.\u itemsControl代码>
应该是这样的:
this.\u contentPresenter.Content=loss.\u itemsControl代码>DataContext=“{Binding'}错误。DataContext=“{Binding}正确。你所有的装订都是错的。将其更改为“{Binding}”,但不解析它。此外,DataContext=“{Binding'}”以前在ItemsControl中工作过。当我试图将ItemsControl放在UserControl中,然后在ContentPresenter中显示它时,出现了问题。我的意思是“”符号对我来说似乎很奇怪
<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;
}
}