开发者

WPF Binding Error reported when Binding appears to work fine

开发者 https://www.devze.com 2022-12-28 06:38 出处:网络
I am trying to create a custom TabItem template/style in my WPF 4.0 application (using VS 2010 Pro RTM), but inspite of everything seeming to work correctly, I am noticing a binding error in the trace

I am trying to create a custom TabItem template/style in my WPF 4.0 application (using VS 2010 Pro RTM), but inspite of everything seeming to work correctly, I am noticing a binding error in the trace window.

The resource dictionary XAML I use to style the TabItems of a TabControl is given in full here. (Just create a simple TabControl with several items and apply the given ResourceDictionary to test it out.)

Specifically, the error is occurring due to the following line (discovered through trial and error testing, since Visual Studio isn't actually reporting it at design time.

<TranslateTransform X="{Binding ActualWidth, ElementName=leftSideBorderPath}"/>

The full error given in the trace (Ouput window) is the following:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=ActualWidth; DataItem=null; target element is 'TranslateTransform' (HashCode=35345840); target property is 'X' (type 'Double')

The error occurs on load and is repeated 5 times then (note that I have 3 tab items in my example). It also occurs consistently and repeatedly whenever for the Window is resized, for example - filling the Output window. Perhaps every time the TabItem layout is updated?

And again, though it is not reported, the error very much seems to be due to the fact that I am binding to any element at all, not specifically leftSideBorderPath or the the ActualWidth propertry. For example, changing this line to the following fixes things.

<TranslateTransform X="25"/>

Unfortunately, hard-coding the value isn't really an option.

This issue seems very strange to me in that the binding does appear to be giving the correct results. (Inspecting the X value of the TranslateTransform at 开发者_JS百科runtime clearly shows the correct bound value, and the ClipGeometry when viewed is exactly what it should be.) Neither Visual Studio nor WPF seems to be giving me any more information on the cause of the error perhaps (setting PresentationTraceSources.TraceLevel to High doesn't help), yet the fact that things are working despite the error being reported inclines me to think that this is some fringe-case WPF bug.

As a side note, the Visual Studio WPF designer and XAML editor are giving me a problem with the following line:

<PathGeometry Figures="{Binding Source={StaticResource TabSideFillFigures}}"/>

Although WPF (at runtime) is perfectly happy binding Figures to the TabSideFillFigures string, with the Binding enforcing the use of the TypeConverter, the XAML editor and WPF designer are complaining. All the XAML code for the ControlTemplate is underlined and I get the following errors in the Error List:

Error 9

'{DependencyProperty.UnsetValue}' is not a valid value for the 'System.Windows.Controls.Control.Template' property on a Setter.

C:\Users\Alex\Documents\Visual Studio 2010\Projects\Ircsil\devel\Ircsil\MainWindow.xaml 1 1 Ircsil

Error 10 Object reference not set to an instance of an object.

C:\Users\Alex\Documents\Visual Studio 2010\Projects\Ircsil\devel\Ircsil\Skins\Default\MainSkin.xaml 58 17 Ircsil

Again, to repeat, everything works perfectly well at runtime, which is what makes this particularly odd...

Could someone perhaps shed some light on these issues, in particular the first (which seems to be a potential WPF bug), and the latter (which seems to be a Visual Studio bug). Any sort of feedback or suggestions would be much appreciated!

Edit:

I've uploaded the complete solution (it's a fairly small project) to my server as a ZIP file. You should be able to see the errors straight away, and is ready to test.


Its fairly common to see some of these Binding errors reported in the output. Its just a load-timing issue. To keep the noise out of the output you can turn off the trace messages for a specific binding:

    ...
    xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
    ...

    <Binding ... diag:PresentationTraceSources.TraceLevel="Off" />


I realise that this is an old post, but for any current viewers, try the following:

<TranslateTransform X="{Binding ActualWidth, ElementName=leftSideBorderPath,
    FallbackValue=0.0}" />

This will set the X value to 0.0 before it can read the value from ActualWidth and remove the error.

0

精彩评论

暂无评论...
验证码 换一张
取 消