Reputation: 189
I'm working in WPF, .NET 3.5 Framework.
I have a TabItem with a DataContext of 'Request', a custom object. 'Request' has a property called 'Id'.
The TabItem's content is a user control (which inherits the DataContext, no?) that contains a DataGrid. The DataGrid is displaying a collection of 'Request' objects, one of which is the same 'Request' instance that is the user control's DataContext.
I'm trying to pass the 'Id' property of the current row as well as the 'Id' of the user control's DataContext to compare them.
I've named my user control (x:Name="myRoot"). Here is the xaml of the binding. Remember, this is all located within a "DataGridTemplateColumn" :
<Image x:Name="imgOpen" Source="open.ico" Width="10" Height="10">
<Image.Visibility>
<MultiBinding Converter="{StaticResource IsLinkedRequest}">
<Binding Path="Id" />
<Binding Path="DataContext.Id" ElementName="myRoot" />
</MultiBinding>
</Image.Visibility>
</Image>
Here is the converter:
Dim datagridRecordId As Integer = CInt(values(0))
Dim currentReqId As Integer = CInt(values(1))
If currentReqId <> datagridRecordId Then
Return Visibility.Visible
Else
Return Visibility.Hidden
End If
My problem is that the DataContext.Id binding (values(1) in the converter) always has a value of MS.Internal.NamedObject. It doesn't matter if I try to bind directly to the DataContext (Path="DataContext" ElementName="myRoot") or even just try to bind to a public property (Path="MyProperty" ElementName="myRoot") of the user control itself, the value is ALWAYS MS.Internal.NamedObject.
Is this an issue with the DataGrid? A bug in the MultiValueConverter? Or am I doing something wrong here?
Any help here would be greatly appreciated!
Thanks,
Dave
Upvotes: 2
Views: 1052
Reputation: 41393
If your Image is in a DataTemplate, then that is a different name scope, than everything outside it. So effectively, you can't bind to an element outside of the DataTemplate by name from within the DataTemplate.
You would have to use something like RelativeSource, or pass the information you need into the DataTemplate (via the it's DataContext).
The NamedObject you are getting is probably the BindingExpression.NullDataItem (you can call ToString() on it to verify). But that effectively says it could not find the element in question.
Upvotes: 0