0

My program has a custom DependencyObject to which I bind values of another DependencyObject which are set in code:

    <TabControl
        Grid.Column="1"
        Grid.Row="1">
        <TabItem
            Header="XML">
            <TextBox
                Text="{Binding Asset.Xml, ElementName=window}"
                IsReadOnly="True" />
        </TabItem>
        <TabItem
            Header="Texture">
            <we:DXImage>
                <we:DXImage.Renderer>
                    <we:TextureRenderer
                        Source="{Binding Asset.Image, ElementName=window}" />
                </we:DXImage.Renderer>
            </we:DXImage>
        </TabItem>
    </TabControl>

The TextBox binding to Asset.Xml works flawlessly, also if I replace the xaml of the second item with a TextBox it also displays the content of Asset.Image (a path to an image of type string). The Source Property of the renderer looks like this:

    private static readonly DependencyProperty SourceProperty = DependencyProperty.Register("Source", typeof(string), typeof(TextureRenderer),
        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender, SourceChanged));

    public string Source
    {
        get { return (string)GetValue(SourceProperty); }
        set { SetValue(SourceProperty, value); }
    }

    private static void SourceChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
    {
        // Do stuff
    }

However the SourceChanged event is never called.

I have updated the project on GitHub: https://github.com/Qibbi/WrathEd/tree/master/WrathEd2 the xaml code is located in the WrathEd2 project while the DXImage, Renderer, and other support classes are in WrathEd.Windows The current MainWindow is a mess code behind wise atm, I plan to refactor it into appropriate parts when finishing the project.

Jana
  • 1
  • 1
  • In your Source binding, try adding "Mode=TwoWay" ? – Adam Sears Jul 13 '15 at 16:14
  • Are you observing any binding error message in the Output Window in Visual Studio when you run your application? – Clemens Jul 13 '15 at 20:04
  • System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=window'. BindingExpression:Path=Asset.Image; DataItem=null; target element is 'TextureRenderer' (Name=''); target property is 'Source' (type 'String') But this is also not the case when using a TextBox – Jana Jul 13 '15 at 21:47

2 Answers2

0

The problem is that your we:TextureRenderer is not a part of the VisualTree (as it is inside a property). Therefore, the binding cannot find the source Element.

According to ElementName Binding is failing, you can use

Source={x:Reference window}

instead of ElementName=window.

Community
  • 1
  • 1
LionAM
  • 1,271
  • 10
  • 28
-1

According to the MSDN documentation here

You need to set NotifyOnSourceUpdated to true for your binding

user2371744
  • 13
  • 1
  • 4
  • Can whoever down voted explain why? Trying to improve my answers! – user2371744 Jul 13 '15 at 16:58
  • I don't know, wasn't me, but still this didn't solve the problem :/ – Jana Jul 13 '15 at 20:02
  • The Source property in the question has nothing to do with a "binding source". It's just that both are called "Source". As OP has stated in the question, the binding actually works (at least when the target is a TextBox). So this answer doesn't make much sense. – Clemens Jul 13 '15 at 20:35