-1

I'm generating bitmaps in the Code and want to display it on the GUI. But the <ImageBrush> is not showing them as BitmapSource/ImageSource. Just like as an invisible image in the 3D part. On the <Image> (not the 3D part) it shows as wanted.

I also tried to create the DiffuseMaterial and ImageBrush in the codebehind and bind them directly into <h:RectangleVisual3D> as Material. And tried to use <VisualBrush> instead of <ImageBrush> with <Image> as child in XAML.

The following Code reproduce the issue for my case:

MainWindow.xaml

<Window x:Class="WPF3DTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPF3DTest"
        xmlns:h="http://helix-toolkit.org/wpf"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <h:HelixViewport3D Grid.Row="0">
            <h:DefaultLights />
            <h:RectangleVisual3D Length="100" Width="100">
                <h:RectangleVisual3D.Material>
                    <DiffuseMaterial>
                        <DiffuseMaterial.Brush>
                            <ImageBrush Opacity="1" ImageSource="{Binding Image}" />
                        </DiffuseMaterial.Brush>
                    </DiffuseMaterial>
                </h:RectangleVisual3D.Material>
            </h:RectangleVisual3D>
        </h:HelixViewport3D>
        <Image Grid.Row="1" Source="{Binding Image}" />
    </Grid>
</Window>

MainWindow.xaml.cs

namespace WPF3DTest
{
    public partial class MainWindow : Window
    {
        public ImageSource Image { get; set; }

        public MainWindow()
        {
            InitializeComponent();

            DataContext = this;

            Image = new BitmapImage(new Uri("C:\\TypenschildDirectory\\Graphic\\laufrichtung2.png"));
            Image.Freeze();
        }
    }
}

The new BitmapImage(new Uri(...)) is a test in my case. But for my purpose it's generated bitmaps by using Imaging.CreateBitmapSourceFromHBitmap(...).

Bergi9
  • 1
  • 3
  • 2
    `Image` needs to be a dependency property, so it will notify the binding in the XAML when its value changes, so that the binding will know when to update the target property. Note that `DataContext = this` is relatively harmless on a Window as you have here, but bad practice in UserControls, because it will break any other bindings. So it's better to get in the habit of using RelativeSource bindings instead. – 15ee8f99-57ff-4f92-890c-b56153 May 09 '19 at 14:58
  • In my real application the exemplary `Image` is in a ViewModel with NotifyPropertyChanged. Registering dependency property in a ViewModel? – Bergi9 May 09 '19 at 15:08
  • Unfortunately, I'm not able to help you diagnose issues in code you haven't shown me. Good luck. – 15ee8f99-57ff-4f92-890c-b56153 May 09 '19 at 15:12

1 Answers1

-2

I'm not sure if it's the correct answer but it solved my problem. Providing a "DataProxy"/"BindingProxy" solved the binding for the <ImageBrush>.

As far I understand from this link https://thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/ is that the <ImageBrush> does not has inherited the DataContext.

<h:HelixViewport3D.Resources>
    <wpf:BindingProxy x:Key="ImageProxy" Data="{Binding Image}" />
</h:HelixViewport3D.Resources>
...
<ImageBrush Opacity="1" ImageSource="{Binding Data, Source={StaticResource ImageProxy}}" />
Bergi9
  • 1
  • 3