wpf - 将 FrameworkElement 及其 DataContext 保存到图像文件没有成功

标签 wpf render datacontext

我有一个名为 UserControl1 的简单 UserControl,其中包含一个 TextBlock:

  <UserControl x:Class="WpfApplication2.UserControl1"
         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>
         <TextBlock Text="{Binding}"/>
     </Grid>
</UserControl>

我初始化了它的一个新实例,并在代码中给它一个 DataContext。当窗口关闭时,我必须将此控件绘制到图像文件中。
UserControl 不会呈现已创建文件中的有界文本。

enter image description here

这是我使用用户控件的代码:
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        Closing += MainWindow_Closing;
    }

    void MainWindow_Closing(object sender, CancelEventArgs e)
    {
        UserControl1 uc = new UserControl1();
        uc.DataContext = "hello";
        uc.Height = 100;
        uc.Width = 100;
        uc.Background = Brushes.LightBlue;
        DrawToImage(uc);
    }

    private void DrawToImage(FrameworkElement element)
    {
        element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
        element.Arrange(new Rect(element.DesiredSize));

        RenderTargetBitmap bitmap = new RenderTargetBitmap((int)element.Width, (int)element.Height,
                                                           120.0, 120.0, PixelFormats.Pbgra32);
        bitmap.Render(element);

        BitmapEncoder encoder = new PngBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(bitmap));

        using (Stream s = File.OpenWrite(@"C:\555.png"))
        {
            encoder.Save(s);
        }
    }
}

我希望它足够清楚,任何帮助将不胜感激。

最佳答案

您只是在手动测量/排列控件后忘记强制对控件进行布局更新(这不足以强制绑定(bind)解析)。

一个简单的电话 UpdateLayout让它工作:

private void DrawToImage(FrameworkElement element)
{
    element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
    element.Arrange(new Rect(element.DesiredSize));
    element.UpdateLayout();

    RenderTargetBitmap bitmap = new RenderTargetBitmap((int)element.Width, (int)element.Height,
                                                        120.0, 120.0, PixelFormats.Pbgra32);
    bitmap.Render(element);

    BitmapEncoder encoder = new PngBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(bitmap));

    using (Stream s = File.OpenWrite(@"C:\555.png"))
    {
        encoder.Save(s);
    }
}

编辑:更多关于何时解决绑定(bind):link

关于wpf - 将 FrameworkElement 及其 DataContext 保存到图像文件没有成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14057763/

相关文章:

arrays - React 重新渲染循环

javascript - componentDidMount 中的多个 setState

.net - 使用设计时的声明性数据上下文和运行时的数据模板连接 MVVM View 和 View 模型?

c# - ListBox ItemsSource 动态过滤器 WPF

c# - 如何通过拖动扩展窗口框架使 WPF 窗口可移动?

ruby-on-rails - 一次渲染 n 个对象,插入代码然后重复接下来的 n 个对象 - Rails

c# - 如何区分 ViewModel 中相似的 UserControl?

c# - WPF 绑定(bind) - 标签绑定(bind)问题

WPF 堆栈面板可见性动画

wpf - 在资源字典中的样式中绑定(bind) Setter 值