How can I access a DataSource object in code behind

Answered How can I access a DataSource object in code behind

  • 2006년 6월 16일 금요일 오후 6:40
     
     

    I specify the object for my DataSource in XAML like this.

    <Grid.Resources>

      <c:SpectralDeviceFilterBinding x:Key="DataSource"/>

    </Grid.Resources>

    I would like to access that object (SpectralDeviceFilterBinding) from code behind, but can't determine how to name or otherwise reference the instance that the page creates when it loads.

    How To?

    Thanks

     

     

     

     

모든 응답

  • 2006년 6월 16일 금요일 오후 6:49
    중재자
     
     
    Did you try FindResource("DataSource")?
  • 2006년 6월 16일 금요일 오후 8:58
     
     

    That throws an exception

    System.Windows.ResourceReferenceKeyNotFoundException was unhandled by user code
      Message="'DataSource' resource not found."

    This works

      TheGrid.TryFindResource("DataSource");

     

     

     

  • 2006년 6월 19일 월요일 오후 6:29
     
     
    Any thoughts/explanation on FindResource not working?
  • 2006년 6월 19일 월요일 오후 7:25
    중재자
     
     답변됨
    FindResource looks into the current object resources (Window/Page) and then Application's resources etc. It will not find anything because the resource is defined in the Grid - and this is why TheGrid.FindResource works.

    You should check the documentation to see how the resource lookup works:

    http://windowssdk.msdn.microsoft.com/en-us/library/ms750613(VS.80).aspx


  • 2006년 6월 19일 월요일 오후 8:43
     
     

    I was simply following the suggestion in the initial reply to my question, which did get me to the right answer at least.

    Thanks for the info and doc link.