locked
Binding to an element inside a data template? RRS feed

  • Question

  • Hi,

    I have this style, where the VerticalOffset of the Style needs to be bound to the height of the border inside the template of that style:

    <Style TargetType={x:Type myCustomContextMenu}>

     <Setter Property="VerticalOffset" Value={<BINDING HERE>}>

     <Setter Property="Template">

      <Setter.Value>

       <ControlTemplate TargetType={x:Type myCustomContextMenu}>

          <Border Name="MyBorder">

       .........some code here.......

          </Border>

    </Setter.Value>

    </Style>

    How do I bind this?

    I tried a lot of thing but nothing works.

    I thought of adding a Code behind file to this ResouceDictionary. I added a Loaded event to the Border and tried using DependencyObject but that doesn't work as I can't inherit from DependencyObject.

    Any idea?


    Please Mark as Answered if this answers your question Or UnMark as Answered if it did not. Happy to Help :)

    Thursday, March 22, 2018 7:08 AM

All replies

  • You could maybe go the brute force approach and add an attached property.

    Bind both to the one property with different modes.

    The border height would be onewaytosource; verticaloffset oneway. 

    Try it with just the Tag of your control first to prove the concept.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Thursday, March 22, 2018 10:58 AM
  • Hi vijaykamat,

    Why do you don't bind VerticalOffset property in the Border using TwoWay mode? I think it is easy to do.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 26, 2018 2:13 AM