开发者

Trying to Access User Control's DataContext from DataGridTemplateColumn using MultiValueConverter; MS.Internal.NamedObject Returned Every Time

开发者 https://www.devze.com 2023-02-27 16:53 出处:网络
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\'.

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 o开发者_StackOverflowf 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


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.

0

精彩评论

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

关注公众号